javascript - jQuery($) 在模块文件中不可用

标签 javascript jquery node.js webpack

我正在使用 webpack 来打包我的代码。 这是我正在加载 jQuery 的 main.js 文件的片段

ma​​in.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/

相关文章:

javascript - ajax 调用结束并返回新数据时如何阻止或处理用户输入

javascript - Jquery 循环追加内容

javascript - node.js 无法解析 ISOString 日期?

apache - 如何将多个域路由到多个 Node 应用程序?

node.js - Node.js 中的单引号还是双引号?

javascript - 使用 lodash 合并连接值的对象

javascript - 在浏览器地址栏中显示内容输入?

javascript - 如果单击其他地方,如何将元素样式恢复正常?

javascript - Chrome 扩展 : Running Content Editor on Client Side

javascript - 如何在按下提交按钮时更改表单的操作 ||让提交按钮只能点击一次?