简体   繁体   English

使用webpack.ProvidePlugin时未定义$

[英]$ is not defined when use webpack.ProvidePlugin

I'm trying to use jquery with webpack but even though I use Webpack Provider plugin like: 我正在尝试使用jquery和webpack,但即使我使用Webpack Provider插件,如:

new webpack.ProvidePlugin({
    'window.jQuery': 'jquery',
    'window.$': 'jquery',
    $: 'jquery'
})

Seems like does not expose my jQuery to window global scope. 似乎不会将我的jQuery暴露给窗口全局范围。

As I understand it, the ProvidePlugin provides those variables to other WebPacked code but not external code or inline scripts. 据我了解,在ProvidePlugin提供这些变量对其他WebPacked代码,但没有外部代码或内嵌脚本。

The 'expose-loader' plugin alternately exposes global variables outside of the webpacked bundles. 'expose-loader'插件交替地在webpacked包之外公开全局变量。 Add the following to your rules to define global jQuery and $ variables that point to your bundled jQuery. 将以下内容添加到规则中以定义指向捆绑jQuery的全局jQuery和$变量。

rules:[
...
// Expose jquery globally for inline/legacy use
{
    test: require.resolve('jquery'),
    use: [
        { loader: 'expose-loader', options: 'jQuery' },
        { loader: 'expose-loader', options: '$' }
    ]
},
...
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM