简体   繁体   English

如何在 angular.json 中包含的 javascript 文件中使用 jquery

[英]how to use jquery in javascript file included in angular.json

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.我正在使用一个在其中使用 jquery 的 javascript 文件..要使用这个 js 文件,我将它放在 angular.json 文件的脚本数组中,并将 jquery 也放在这个文件之前的同一个数组中。 but when i run the angular project it get me但是当我运行 angular 项目时,它让我明白了

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.文件 theme.js 是在其中使用 jquery 的文件。

To Install JQuery follow below steps要安装JQuery按照以下步骤操作

  1. install jQuery using npm as使用 npm 作为安装 jQuery

    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导航到 Angular CLI 项目文件夹根目录下的 ./angular-cli.json 文件,找到 scripts: [] 属性,并包含 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现在你要做的就是将它导入到任何你想使用 jQuery 的组件中

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

Assuming you have installed JQuery using npm install jquery You need to additionally install types as well.假设您已经使用npm install jquery安装了 JQuery 您还需要另外安装类型。

Install types for jquery using使用 jquery 安装类型

npm install --save-dev @types/jquery

and for using jquery in your component you need to use :要在您的组件中使用 jquery,您需要使用:

import * as $ from 'jquery';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM