简体   繁体   English

带有 webpack (SignalR) 的 jQuery 和插件

[英]jQuery and plugins with webpack (SignalR)

I'm trying to use a JQuery Plugin ( SignalR ) inside my React application.我正在尝试在我的 React 应用程序中使用 JQuery 插件 ( SignalR )。

What I did is :我所做的是:

  1. Install jquery via npm ;通过 npm 安装 jquery;
  2. Install the signalr client via npm ;通过 npm 安装信号客户端;
  3. Add entries in webpack.config to specify jQuery and SignalR paths ;在 webpack.config 中添加条目以指定 jQuery 和 SignalR 路径;
  4. Use import $ from 'jquery' and import 'msSignalrClient' in the React component I want to.在我想要的 React 组件中使用import $ from 'jquery'import 'msSignalrClient'

But that doesn't seem to work as I get this error : jquery.signalR.min.js?dc9f:9Uncaught Error: jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file.但这似乎不起作用,因为我收到此错误: jquery.signalR.min.js?dc9f:9Uncaught Error: jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file. jquery.signalR.min.js?dc9f:9Uncaught Error: jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file.

One workaround that works is to load in jQuery like this inside my index.html : <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> .一种有效的解决方法是在我的index.html像这样加载 jQuery : <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> Of course it's not optimal at all, and I'd like to import jQuery with the method I explained above.当然它根本不是最优的,我想用我上面解释的方法导入jQuery。 Any help appreciated !任何帮助表示赞赏!

this should work:这应该有效:

npm install jquery signalr expose-loader --save

//inside vendor.ts
import 'expose-loader?jQuery!jquery';
import '../node_modules/signalr/jquery.signalR.js';

我最后做的是这样的:

window.$ = window.jQuery = require("jquery");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM