未定义jQuery:webpack +Angular 4+ Asp Core模板



我有一个从VS 2017 Angular 4 + Asp Core模板生成的项目我想使用 jQuery 插件,所以我需要导入 jQuery,在我的 .ts 文件中我使用了:

declare var $: any;

在 webpack.config.vendor 中.js我有

plugins: [
            new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), 

但在控制台中我收到此错误:

Uncaught ReferenceError: jQuery is not defined  

谢谢

你需要使用 angular-cli 导入 jquery。编辑您的 angular-cli.json 文件。

查找脚本数组并添加 jquery。

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

按照下面的链接安装 jquery。

https://www.gurustop.net/blog/2016/11/18/3821/jquery-angular2-angularcli-how-to

之后,您需要添加:

 declare const $: JQueryStatic;

在您的 app.component.ts 中可用于您的所有应用程序。

最新更新