Closed simplecooler closed 8 years ago
@simplecooler 直接用 var $ = require("jquery");
这样引入之后,在各页面中的js文件里使用$
会报ReferenceError: Can't find variable: $
,感觉没有全局引入,而且这样引入jquery
之后跑npm run production
会有两个warning
。
@simplecooler 常规的CommonJs风格的用法是任何时候需要用到jquery
,都require()
一次。 不过,全局准备好jquery
可以更方便。推荐的做法是在webpack的配置文件的plugins
增加以下内容:
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
这是webpack的插件,详情可以参照官方文档。这里表示把require('jquery')
的内容作为所有bundle module共用的内容,输出为$
和jQuery
两个变量。
谢谢帮助,现在终于都理顺了,开发流很不错。
在
commom.js
中require("node_modules/jquery/dist.jquery.js");
会报错