我正在使用 webpack 来打包我的代码。 这是我正在加载 jQuery 的 main.js 文件的片段
main.js
var $ = global.jQuery = require('jquery)
$('someSelctor').on('rest of the code.') // This is working fine
var importFile = require('someExport.js')
importFile.someFunction();
someExport.js
module.exports = {
someFunction :function(){
$('someSelector').on(' some other code') // Error here. $ is not a function
}
}
我还尝试通过加载 jquery,如 someExport.js
中的 require('jquery')
。但这并没有解决问题。
由于 jquery
已经由 main.js
加载,我是否需要再次加载它。另外如何在 someExport.js 文件中使用 jquery
谢谢
最佳答案
在 someExport.js
中需要 jQuery :
var $ = require(jquery);
module.exports = {
someFunction :function(){
$('someSelector').on(' some other code') // Error here. $ is not a function
}
}
关于javascript - jQuery($) 在模块文件中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36442253/