简体   繁体   English

带有Webpack的ScrollReveal-ReferenceError:未定义ScrollReveal

[英]ScrollReveal with webpack - ReferenceError: ScrollReveal is not defined

Using Laravel 5.6.29 with laravel-mix@2.1.11 & webpack@3.12.0 . Laravel 5.6.29laravel-mix@2.1.11webpack@3.12.0结合使用

After installing scrollreveal@3.4.0 via npm i scrollreveal and requiring it in my app.js with require('scrollreveal') , I see that in my app.js output that ScrollReveal is there to be found. 通过npm i scrollreveal安装scrollreveal@3.4.0并使用require('scrollreveal')在我的app.js中要求它之后,我发现在app.js输出中可以找到ScrollReveal

However, when I try to initiate it with window.sr = ScrollReveal(); 但是,当我尝试使用window.sr = ScrollReveal();初始化它时window.sr = ScrollReveal(); it throws me an error: ReferenceError: ScrollReveal is not defined , even though the reference comes after the definition. 它引发了一个错误: ReferenceError:ScrollReveal未定义 ,即使引用位于定义之后。

I tried it with two different ways. 我用两种不同的方法尝试过。 First is to: 首先是:

resources/assets/js/app.js 资源/资产/ JS / app.js

require('scrollreveal');
window.sr = ScrollReveal();

or the second way which is: 或第二种方式是:

resources/views/index.blade.php 资源/视图/ index.blade.php

<script type="text/javascript" src="{{ mix('/js/app.js') }}"></script>
<script type="text/javascript">
window.sr = ScrollReveal();
</script>

However, if I just include the js the traditional way: 但是,如果我仅以传统方式包括js:

<script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script>

and then make a call to ScrollReveal(), it actually works. 然后调用ScrollReveal(),它实际上可以工作。

Perhaps it's somehow a scope problem? 也许这是一个范围问题? But why would it only appear with ScrollReveal and not any other libraries I use? 但是,为什么它只在ScrollReveal中出现而不在我使用的任何其他库中出现? And isn't NodeJS require() function importing to global scope? 而且NodeJS require()函数不会导入全局范围吗?

UPDATE: Third way I found that fixes this problem is by changing resources/assets/js/app.js and adding: 更新:我发现解决此问题的第三种方法是更改资源/资产/js/app.js并添加:

import ScrollReveal from 'scrollreveal';
window.sr = ScrollReveal();

will fix the issue, as this will in-scope define window.sr as ScrollReveal() using ES6 import (appearantly). 将解决此问题,因为这将使用ES6 import( 显然)在范围内将window.sr定义为ScrollReveal( )。

However, why wouldn't NodeJS require work still? 但是,为什么NodeJS仍然不需要工作?

Made it work, in a way I don't yet know why it works (do elaborate if you know!), but it works. 以某种方式使它起作用了,我尚不知道为什么它起作用(如果您知道,请进行详细说明!),但是它起作用了。

Changing resources/assets/js/app.js and adding: 更改资源/资产/js/app.js并添加:

require('scrollreveal')().reveal('.sreveal');

fixes the issue, using the NodeJS require(). 使用NodeJS require()解决了该问题。

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

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