簡體   English   中英

如何使用Webpack包括jQuery和jQuery驗證?

[英]How to include jQuery and jQuery Validation using Webpack?

我是Webpack的新手,正在嘗試使用https://github.com/erikras/react-redux-universal-hot-example創建一個Universal React應用。 我使用以下方法包含了jQuery:

var jquery = new webpack.ProvidePlugin({
  $: "jquery",
  jQuery: "jquery"
})

並將其包含在module.exports插件數組中以使其全局。 在我的JS文件中,我試圖像這樣包含它

import $ from 'jquery';
import jqueryValidation from 'jquery-validation';

但是我在NodeJS中收到以下錯誤:

> error given was: TypeError: $.extend is not a function  
[1]     at /Users/admin/repo/learnReact/node_modules/jquery-validation/dist/jquery.validate.js:19:3  
[1]     at /Users/admin/repo/learnReact/node_modules/jquery-validation/dist/jquery.validate.js:13:20  
[1]     at Object.<anonymous> (/Users/admin/repo/learnReact/node_modules/jquery-validation/dist/jquery.validate.js:17:2)  
[1]     at Module._compile (module.js:570:32)  
[1]     at Module._extensions..js (module.js:579:10)  
[1]     at require.extensions.(anonymous function) (/Users/admin/repo/learnReact/node_modules/babel-register/lib/node.js:152:7)  
[1]     at Object._module2.default._extensions.(anonymous function) [as .js] (/Users/admin/repo/learnReact/node_modules/require-hacker/babel-transpiled-modules/require hacker.js:260:68)  
[1]     at Module.load (module.js:487:32)  
[1]     at tryModuleLoad (module.js:446:12)  
[1]     at Module._load (module.js:438:3)  
[0] ./src/utils/validation.js

我究竟做錯了什么? 在線快速搜索后,我還嘗試了一些帖子建議的Exposure-loader,但似乎也沒有用。 修復此問題的任何幫助或對良好Webpack教程的任何引用將受到高度贊賞。

jQuery只能在DOM上使用。 驗證也是如此

https://www.npmjs.com/package/jquery#node

因此,如果要包含jquery,則必須使用腳本標記將其注入到客戶端輸入文件(webpack)中

這樣,jquery將始終在具有DOM的環境中運行。 之后,您將可以在react應用中通過window。$使用jquery。

(componentDidMount) (componentWillMount does not support DOM afaik)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM