【问题标题】:jQuery is not defined: webpack +Angular 4+ Asp Core templatejQuery 未定义:webpack +Angular 4+ Asp Core 模板
【发布时间】:2017-10-25 07:15:37
【问题描述】:

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

declare var $: any;

在 webpack.config.vendor.js 我有

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

但是在控制台中我得到了这个错误:

Uncaught ReferenceError: jQuery is not defined  

谢谢

【问题讨论】:

    标签: jquery angular webpack asp.net-core


    【解决方案1】:

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

    找到脚本数组并添加jquery。

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

    【讨论】:

      【解决方案2】:

      点击下面的链接安装jquery。

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

      之后你需要添加:

       declare const $: JQueryStatic;
      

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

      【讨论】:

      • 嗨,我已经完成安装,但没有 angular-cli.json 文件,也在:declare const $: JQueryStatic;我得到了:找不到名称'JQueryStatic'。谢谢
      • 你使用 angular cli 吗?
      • 似乎 jquery 是用 import * as $ from 'jquery' 导入的;但它给我的插件错误: $(this.el.nativeElement).find('#passw').keyboard({ (TS) Property 'keyboard' does not exist on type 'JQuery'
      • 你可以将它投射到任何... ($(this.el.nativeElement).find('#passw')).keyboard({})
      猜你喜欢
      • 1970-01-01
      • 2018-04-12
      • 2017-07-12
      • 1970-01-01
      • 2018-02-17
      • 2020-07-25
      • 1970-01-01
      • 2018-05-26
      • 2021-06-28
      相关资源
      最近更新 更多