【问题标题】:Upgraded to angular 6 and jQuery stopped working升级到 Angular 6 和 jQuery 停止工作
【发布时间】:2018-10-26 11:38:32
【问题描述】:

我刚刚从Angular 5.2.5 升级到Angular 6.0.1Webpack 4.8.3。但是,当我尝试加载使用 Jquery 的页面时,我的控制台中会出现 $(...).func is not a function

我通过我的 webpack 配置在我的整个应用程序中导入 jquery:

plugins: [
    new webpack.ProvidePlugin({
        jQuery: "jquery",
        $: "jquery",
        jquery: "jquery",
        "window.jQuery": "jquery"
    }),
],

到目前为止,我还没有发现任何改变,即使在 documentation 中它看起来也一样,但自从升级后它就无法正常工作了。

有什么解决办法吗?

【问题讨论】:

  • 你不应该在 Angular 中使用 jQuery。这是不好的做法

标签: javascript jquery angular webpack


【解决方案1】:

你试过暴露加载器吗?

有一个如何添加 jQuery 的示例: https://github.com/webpack-contrib/expose-loader

我自己在 vendor.ts 中使用它:

declare var jQuery: any;
declare var $: any;

import 'expose-loader?$!expose-loader?window.jQuery!jquery';

(<any>window).jQuery = $;

【讨论】:

  • 这需要包含更多细节或者是评论
猜你喜欢
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-04
相关资源
最近更新 更多