【问题标题】:Laravel Mix (webpack) Inline JS undefined jQuery/vendorsLaravel Mix (webpack) Inline JS undefined jQuery/vendors
【发布时间】:2018-09-23 23:13:34
【问题描述】:

在为看似非常常见的问题寻找解决方案数小时后,对此做噩梦,目前只是在兜圈子。

我最近将我的 Laravel 项目从使用 gulp 更改为 Laravel Mix、SASS 等,但我的 JS 无法正常运行。

在刀片/php 模板中使用 jQuery * inline * 的任何操作都失败:

Uncaught ReferenceError: jQuery is not defined

webpack.mix.js(带有一系列模块):

let mix = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js').extract(
  ['jquery', 'foundation-sites', 'vue', 'toastr', 'jscroll', 'chosen-js', 'what-input']
);

mix.sass('resources/assets/sass/app.scss', 'public/css');

ma​​ster.blade.phpextract method 将它们分开):

<script type="text/javascript" src="/js/manifest.js"></script>
<script type="text/javascript" src="/js/vendor.js"></script>
<script type="text/javascript" src="/js/app.js"></script>

app.js

const jQuery = require('jquery');
// window.$ = global.jQuery = $;
// window.jQuery = jQuery;
const toastr = require('toastr');
const chosen = require('chosen-js');
const whatInput = require('what-input');

const Foundation = require('foundation-sites');
const Vue = require('Vue');
const jScroll = require('jscroll');
const mousetrap = require('mousetrap');

我尝试过导入,直接要求,离开要求并使用自动加载方法,似乎没有任何效果。一些变体会说 $ 也没有定义,只是不知道该去哪里。

有人可以指导我正确的方向吗?多年来一直使用 Laravel 和 gulp,但只使用 vanilla JS,这个 webpack 让我很难过。

【问题讨论】:

    标签: javascript php laravel webpack laravel-mix


    【解决方案1】:

    在您的app.js 中,在行尾插入以下内容

    const jQuery = require('jQuery')
    // other packages here
    
    window.$ = window.jQuery = jQuery
    

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 1970-01-01
      • 2021-08-25
      • 2019-04-05
      • 2021-09-17
      • 2018-07-29
      • 1970-01-01
      • 2017-10-14
      • 2021-08-24
      相关资源
      最近更新 更多