Laravel 5.6-Webpack:未捕获的ReferenceError:jQuery未定义

时间:2018-07-27 15:29:07

标签: laravel laravel-5 npm webpack laravel-5.6

我收到了package.json:

some.exe

使用webpack.mix:

  "devDependencies": {
    "axios": "^0.18.0",
    "bootstrap": "^4.1.3",
    "bootstrap-datepicker": "^1.7.1",
    "browser-sync": "^2.24.6",
    "browser-sync-webpack-plugin": "^2.0.1",
    "chart.js": "^2.7.2",
    "cross-env": "^5.2.0",
    "datatables": "^1.10.18",
    "easy-pie-chart": "^2.1.7",
    "font-awesome": "4.7.0",
    "fullcalendar": "^3.9.0",
    "jquery": "^3.3.1",
    "jquery-sparkline": "^2.4.0",
    "jvectormap": "^2.0.4",
    "laravel-mix": "^2.1.11",
    "load-google-maps-api": "^1.2.0",
    "lodash": "^4.17.10",
    "masonry-layout": "^4.2.2",
    "perfect-scrollbar": "^1.1.0",
    "popper.js": "^1.14.3",
    "skycons": "^1.0.0",
    "vue": "^2.5.16"
  }

和资源文件:

enter image description here

所以这些文件看起来像是由webpack正确编译的>> mix.sass('resources/assets/styles/index.scss', 'public/css/app.css') .js('resources/assets/scripts/index.js', 'public/js/app.js') .copyDirectory('resources/assets/static', 'public/static') .version() .sourceMaps(); 然后将其加载到刀片模板,但随后在浏览器中显示: jQuery未定义 ...

  

未捕获的ReferenceError:未定义jQuery

这里可能出了什么问题,为什么当我在package.json中定义它时可以加载jQuery?

谢谢!

1 个答案:

答案 0 :(得分:3)

我发布问题几分钟后...

这是答案...

将此添加到您的Webpack:

mix.webpackConfig(webpack => {
    return {
        plugins: [
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery',
                'window.jQuery': 'jquery',
            })
        ]
    };
});