简体   繁体   English

使用 webpack Laravel 5.4 加载物化时的 jQuery 错误

[英]jQuery error on loading materialize with webpack Laravel 5.4

I would like use http://materializecss.com/ on Laravel 5.4 (compiled with webpack).我想在 Laravel 5.4(用 webpack 编译)上使用http://materializecss.com/

window.$ = window.jQuery = require('jquery');

require('materialize-css');
// or
// require('materialize-css/dist/js/materialize.js');
// but same issue

$(document).ready(function(){
    $('.materialboxed').materialbox();
    $('.modal').modal();
    $('.parallax').parallax();
});

This is my code but the error is:这是我的代码,但错误是:

Uncaught TypeError: $(...).materialbox is not a function未捕获的类型错误:$(...).materialbox 不是函数

It work with this :它适用于:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="{{ asset('js/materialize.min.js') }}"></script>
<script>
   $(document).ready(function(){
      $('.materialboxed').materialbox();
      $('.modal').modal();
      $('.parallax').parallax();
   });
</script>

But if I put jquery document ready in app.js, it doesn't work :但是如果我在 app.js 中jquery document ready ,它就不起作用:

 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
 <script src="{{ asset('js/materialize.min.js') }}"></script>
 <script src="{{ mix('js/app.js') }}"></script>

app.js :应用程序.js:

$(document).ready(function(){
    $('.materialboxed').materialbox();
    $('.modal').modal();
    $('.parallax').parallax();
});

I also had this kind of issue with materialize-css in Laravel 5.4, and my quick fix was to download the materializecss source to /resources/assets/ manually. 我在Laravel 5.4中也遇到了materialize-css的问题,我的快速解决方案是手动将materializecss源下载到/ resources / assets /。 This is what my directory looks like now, 这是我的目录现在的样子

/resources
----/js
-------app.js
-------bootstrap.js
----/materialize
-------/css
-------/fonts
-------/js

Then, on my bootstrap.js , I added this code 然后,在我的bootstrap.js ,添加了以下代码

require('../materialize/js/materialize.js');

And this solved my problem. 这解决了我的问题。

The way to include javascript in blades (in laravel) is: 在刀片中包含javascript的方式(在laravel中)是:

<script src="{{ asset('assets\bower\jquery\dist\jquery.min.js') }}"></script>
<script src="{{ asset('assets\bower\materialize\dist\js\materialize.min.js') }}"></script>

Your assests (js & css) have to be located somewhere under /public 您的资产(js和CSS)必须位于/public下的某个位置

I would recommend using bower for your assetmanagement, but this is not required. 我建议您在资产管理中使用Bower,但这不是必需的。

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

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