jQuery($)在模块文件中不可用
我正在使用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像require('jquery')
在someExport.js
。 但是这并不能解决问题。 由于jquery
已经由main.js
加载,我需要再次加载它。也可以如何使用jquery在someExport.js文件
谢谢
在someExport.js
需要jQuery:
var $ = require(jquery); module.exports = { someFunction :function(){ $('someSelector').on(' some other code') // Error here. $ is not a function } }
也许你可以添加这个在你的模块?
var script = document.createElement("script"); script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"); document.body.appendChild(script);
这通过创build特定的脚本来强制添加jQuery。
但不知道它被优化(我用它Tampermonkey脚本)。