简体   繁体   English

文件中的 Laravel javascript 不起作用,但如果通过 CDN 包含则起作用

[英]Laravel javascript in file doesn't work, but does work if included through CDN

This works:这有效:

<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.0.2/anime.min.js"></script>

<script type="text/javascript">
var textWrapper = document.querySelector('.ml6 .letters');
textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");

anime.timeline({loop: false})
    .add({
    targets: '.ml6 .letter',
    translateY: ["1.1em", 0],
    translateZ: 0,
    duration: 750,
    delay: (el, i) => 50 * i
});
</script>

But if I include the exact JS from the CDN in libraries.js as shown below, I get ReferenceError: Can't find variable: anime .但是,如果我在libraries.js包含来自CDN 的确切JS,如下所示,我得到ReferenceError: Can't find variable: anime

<script src="{{ mix('/js/libraries.js') }}"></script>

<script type="text/javascript">
var textWrapper = document.querySelector('.ml6 .letters');
textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");

anime.timeline({loop: false})
    .add({
    targets: '.ml6 .letter',
    translateY: ["1.1em", 0],
    translateZ: 0,
    duration: 750,
    delay: (el, i) => 50 * i
});
</script>

I checked and mix is compiling as it should, the JS is included in libraries.js .我检查了一下,mix 正在编译,JS 包含在libraries.js

It's the exact same script, included in the exact same location so I'm puzzled as to why this is not working.这是完全相同的脚本,包含在完全相同的位置,所以我很困惑为什么这不起作用。 The only thing I can think of is that libraries.js is loaded after the script is ran.我唯一能想到的是libraries.js脚本运行加载的。 Is that the case?是这样吗? If so: how do I solve this?如果是这样:我该如何解决这个问题?

I'm running into this issue more often.我更经常遇到这个问题。 I would like to use mix() instead of asset() .我想使用mix()而不是asset() The above is about 1 library, but I would prefer to include all of most of the libraries I use in the one file libraries.js , but today I'm loading most of them through CDNs because of the issue described above.以上是关于 1 个库,但我更愿意将我使用的所有大多数库都包含在一个文件libraries.js ,但由于上述问题,今天我通过 CDN 加载了其中的大部分。

I'm assuming you have a file resources/js/libraries.js with a bunch of require();我假设你有一个文件resources/js/libraries.js和一堆require(); to import npm packages and another JS file to import custom code?导入npm包和另一个 JS 文件以导入自定义代码? Well I would like to suggest something:好吧,我想提出一些建议:

You don't have to make a separate JS file for them, webpack can extract them automatically like:您不必为它们制作单独的 JS 文件,webpack 可以自动提取它们,例如:

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css')
    .extract([
        'bootstrap', 
        'popper.js',
        'lodash',
        'axios',
        'jquery',
        'vue',
    ]);

mix.version();

Then you put this at the bottom of your layout:然后你把它放在布局的底部:

<script src="{{ mix('js/manifest.js') }}"></script>
<script src="{{ mix('js/vendor.js') }}"></script>
<script src="{{ mix('js/app.js') }}"></script>
@stack('scripts')

vendor.js is basically your libraries.js and app.js is your custom code. vendor.js基本上是您的libraries.jsapp.js是您的自定义代码。

When you mix.version() : If your custom code changes and you npm run prod , only app.js will change (which is a small JS file) and the vendor.js (which is a big file) will stay exactly the same.当你mix.version() :如果你的自定义代码发生变化并且你npm run prod ,只有app.js会发生变化(这是一个小 JS 文件),而vendor.js (这是一个大文件)将保持完全相同. This is a huge advantage as old users will only have to load the small app.js and the vendor.js will still be cached, making the loading faster.这是一个巨大的优势,因为老用户只需要加载小app.js ,而vendor.js仍将被缓存,从而使加载速度更快。


But to answer your question , it's not picking up the variable name anime , so import it like this in your JS file to be recognized:但是要回答你的问题,它没有选择变量名anime ,所以在你的JS文件中像这样导入它以被识别:

window.anime = require('animejs');

尝试npm run watch然后再试一次。

This might help you .这可能对你有帮助。

  1. I have stored the give js file as .min.js format .我已将给定的 js 文件存储为.min.js格式。
  2. Instead of using mix , I have used asset to get the js file .我没有使用mix ,而是使用asset来获取 js 文件。
<script src = "{{asset('js/anime.min.js')}}" type ="text/javascript"> </script>

Please check this.请检查这个。 And I didn't find any issue on this .我没有发现任何问题。

Here is the complete code Which I have tried :这是我尝试过的完整代码:

<script src = "{{asset('js/anime.min.js')}}" type ="text/javascript">
</script>
  <script>
  var textWrapper = document.querySelector('.ml6 .letters');
textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");

anime.timeline({loop: false})
    .add({
    targets: '.ml6 .letter',
    translateY: ["1.1em", 0],
    translateZ: 0,
    duration: 750,
    delay: (el, i) => 50 * i
}).add({
    targets: '.ml6',
    opacity: 0,
    duration: 1000,
    easing: "easeOutExpo",
    delay: 1000
});
</script>

尝试用以下内容包装您的内联 js:

window.addEventListener('load', function(){ // here });

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

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