[英]Webpack - _ is not defined
当我运行webpack时, _
在窗口上返回undefined: window._ ::: Error: _ is not defined
。 我认为将_
放在插件中会将其暴露给window
。 我的理解不正确吗?
const webpack = require('webpack');
const plugins = new webpack.ProvidePlugin({
$: "jquery",
_: "underscore"
});
module.exports = {
entry: {
app: './src/main/app/components/main.module.js',
vendor: [
'jquery',
'underscore'
]
},
output: {
filename: './src/main/resources/dist/app/scripts/[name].bundle.js',
},
plugins: [plugins],
devtool: 'inline-source-map'
};
您是否尝试将其添加为全局变量? 尝试将此添加到.ts / .js文件中:
window['_'] = require('underscore');
providePlugin
用于在webpack包中的其他模块的上下文中使库可用。 例如, providePlugin
这种用法:
const plugins = new webpack.ProvidePlugin({
$: "jquery",
_: "underscore"
});
将允许通过$
变量访问./src/main/app/components/main.module.js
jquery库。
如果你想将jquery作为window.$
显示给窗口,你可以使用webpack的expose-loader ,或者只是更新你的条目文件中的窗口(main.module.js):
window.$ = $; //$ is defined via providePlugin
window._ = _; //_ is defined via providePlugin
你是否在索引文件中加载了underscore.js?
<script src = 'https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js'></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.