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

时间:2017-10-25 07:15:38

标签: jquery angular webpack asp.net-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  

谢谢

2 个答案:

答案 0 :(得分:2)

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

查找脚本数组并添加jquery。

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

答案 1 :(得分:1)

按照以下链接安装jquery。

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

之后你需要添加:

 declare const $: JQueryStatic;
您的app.component.ts中的

可用于您的所有应用程序。