未捕获的TypeError:_particles2.default.load不是函数-React,create-react-app,particleJS [英] Uncaught TypeError: _particles2.default.load is not a function - React, create-react-app, particlesJS
问题描述
在导入 particles.js 库时遇到了麻烦。
Am having trouble importing the particles.js library.
Am使用create-react-app,并遵循js github页面上的说明。
Am using create-react-app, and am following the instructions on the particles js github page also.
启动 create- react-app',我安装了particles.js
After initiating 'create-react-app', I install particles.js
npm install particles.js --save
然后在App.js文件中,我拥有的相关部分是:
then in the App.js file the relevant parts I have are:
import particlesJS from 'particles.js'
componentDidMount(){
//particles.js github page says to load package like so:
particlesJS.load('particles-js', 'assets/particles.json', function() {
console.log('callback - particles.js config loaded');
});
},
render() {
return (
<div>
//div to include particles canvas, as specified in particles.js github page
<div id="particles-js"></div>
</div>
);
}
不过控制台会提供错误
未被捕获的TypeError:_particles2.default.load不是一个函数
However the console gives the error Uncaught TypeError: _particles2.default.load is not a function
如果我将它放在console.log(particlesJS)中,它将给出一个空对象,因此我很确定问题出在我如何装载包裹上。基本上,程序包甚至都没有被加载,这就是为什么particlesJS对象上没有.load()函数的原因。
And if I put to console.log(particlesJS) it gives an empty object, so I am pretty sure the problem is with how I am loading the package. Basically, the package is not even being loaded, that is why there is no .load() function available on the particlesJS object.
有什么办法解决此问题吗?
Any ideas how to fix this?
顺便说一句,已使软件包通过' react-particles-js '程序包,但是仍然好奇如何直接使用package.js程序包使其工作。
By the way, have got the package working through the 'react-particles-js' package, however still curious to see how to get it to work by using the package.js package directly.
推荐答案
该库似乎是在不考虑JavaScript捆绑程序的情况下编写的,因此它实际上无法像您这样使用 import
语句会期望的。而是在窗口
对象上设置全局变量。
It looks like the library was written without regard for JavaScript bundlers, so it's not really working with the import
statement like you would expect. Instead it sets a global variable on the window
object.
因此,要使用它,您可以执行以下操作: / p>
So to use it you could do something like:
import 'particles.js/particles';
const particlesJS = window.particlesJS;
希望这会有所帮助!
这篇关于未捕获的TypeError:_particles2.default.load不是函数-React,create-react-app,particleJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!