我总是收到一条消息说:
$.hubConnection 不是函数
Error: jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file. consoling out "$" and "jquery" is undefined.
我需要做什么,才能让 signalr 使用 webpack 工作?
最佳答案
干净的解决方案:使用 expose-loader !!
npm install expose-loader --save-dev
在你的 vendor.ts 中
import 'expose-loader?jQuery!jquery';
import '../node_modules/signalr/jquery.signalR.js';
在你的 webpack.config.ts 中
new ProvidePlugin({ jQuery: 'jquery', $: 'jquery', jquery: 'jquery' })
解释: jquery.signalR.js 脚本确实没有写成 umd 模块。这使得它在默认情况下不能被 webpack 加载。它不需要 jquery,但假定 Jquery 存在于全局变量 window.jQuery 中。我们可以通过使用 expose-loader 导入 jquery 模块来使它在 webpack 中工作。这个加载器将确保 jquery 的导出值暴露给 jQuery 全局变量。因此允许加载 signalr.js 脚本作为下一个模块。
此外,如果您以后想通过 $ 使用 signalr,您还必须使用 jquery 模块的 provideplugin。在 webpack.config.js 里面
关于angular - 如何在 Angular 2 中的 webpack 中加载 signalr.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40030002/