英文:
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
,请按照以下步骤进行:
-
使用npm安装jQuery:
npm install jquery --save
-
转到位于您的Angular CLI项目文件夹根目录的./angular-cli.json文件,找到scripts: []属性,并包括jQuery的路径:
"scripts": [ "../node_modules/jquery/dist/jquery.min.js" ]
-
现在您需要在希望使用jQuery的任何组件中导入它:
import * as $ from 'jquery';
或者
declare var $: any;
英文:
To Install JQuery
follow below steps
-
install jQuery using npm as
npm install jquery — save
-
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" ]
-
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';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论