如何在包含在angular.json中的JavaScript文件中使用jQuery。

huangapple go评论66阅读模式
英文:

how to use jquery in javascript file included in angular.json

问题

我正在使用一个包含jQuery的JavaScript文件。
为了使用这个JS文件,我将它放在angular.json文件中的脚本数组中,
并且在这个文件之前也将jQuery放在同一个数组中。
但是当我运行Angular项目时,它给我报错:

缺少声明 $
"scripts": [
   "node_modules/jquery/dist/jquery.min.js",
   "node_modules/popper.js/dist/umd/popper.min.js",
   "node_modules/bootstrap/dist/js/bootstrap.min.js",
   "node_modules/crypto-js/crypto-js.js",
   "assets/js/theme.js"
]

theme.js文件是使用jQuery的文件。

英文:

i am using a javascript file that use jquery in it ..
to use this js file i put it in script array in angular.json file
and put also the jquery in the same array before this file.
but when i run the angular project it get me

missing declare for $
"scripts": [
   "node_modules/jquery/dist/jquery.min.js"
   "node_modules/popper.js/dist/umd/popper.min.js",
   "node_modules/bootstrap/dist/js/bootstrap.min.js",
   "node_modules/crypto-js/crypto-js.js",
   "assets/js/theme.js"
]

the file theme.js is the one that use jquery in it.

答案1

得分: 1

要安装JQuery,请按照以下步骤进行:

  1. 使用npm安装jQuery:

    npm install jquery --save
    
  2. 转到位于您的Angular CLI项目文件夹根目录的./angular-cli.json文件,找到scripts: []属性,并包括jQuery的路径:

    "scripts": [ "../node_modules/jquery/dist/jquery.min.js" ]
    
  3. 现在您需要在希望使用jQuery的任何组件中导入它:

    import * as $ from 'jquery';
    

    或者

    declare var $: any;
    
英文:

To Install JQuery follow below steps

  1. install jQuery using npm as

    npm install jquery — save

  2. Navigate to the ./angular-cli.json file at the root of your Angular CLI project folder, and find the scripts: [] property, and include the path to jQuery

    "scripts": [ "../node_modules/jquery/dist/jquery.min.js" ]

  3. Now you have to do is to import it in whatever component you want to use jQuery

    import * as $ from 'jquery';
    (or)
    declare var $: any;
    

答案2

得分: 0

You need to additionally install types as well.
Install types for jQuery using:
npm install --save-dev @types/jquery
And for using jQuery in your component, you need to use:
import * as $ from 'jquery';

英文:

Assuming you have installed JQuery using npm install jquery
You need to additionally install types as well.

Install types for jquery using

npm install --save-dev @types/jquery

and for using jquery in your component you need to use :

import * as $ from 'jquery';

huangapple
  • 本文由 发表于 2020年1月6日 19:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定