简体   繁体   English

将particles.js 包含到rails / webpack 项目中

[英]Include particles.js into rails / webpack project

I have a project based on Rails 6.0.0.rc2 including webpack.我有一个基于 Rails 6.0.0.rc2 的项目,包括 webpack。 I am trying to include simply the library particles.js我试图简单地包含库particles.js

I am following the GitHub readme: - Install the library with yarn - Add a container with a specific id - Try to initialize the function using particlesJS.load我正在关注 GitHub 自述文件: - 使用 yarn 安装库 - 添加具有特定 ID 的容器 - 尝试使用particleJS.load 初始化函数

Obviously, it is not working: particleJS is not defined显然,它不起作用:未定义particleJS

particle_js__webpack_imported_module_0___default.a.load is not a function particle_js__webpack_imported_module_0___default.a.load 不是函数

After reading a lot of questions on this topic but nothing is really clear.在阅读了很多关于这个主题的问题后,但没有什么是真正清楚的。 here are some leads but I did not succeed:这里有一些线索,但我没有成功:

I understood that particleJS is linked to the window object.我知道particleJS 链接到window 对象。 And indeed, when I try in the console particleJS, it works.事实上,当我尝试在控制台中使用particleJS 时,它可以工作。

So could you tell me simply how to use particleJS with webpack?那么你能简单地告诉我如何在 webpack 中使用particleJS吗? Or how to use a window function into webpack?或者如何在webpack中使用窗口函数?

Thank you in advance for your efforts!预先感谢您的努力!

Welcome to the Rails+webpacker mayhem.欢迎来到 Rails+webpacker 混乱。 In theory you would do something like:理论上你会做这样的事情:

let particlejs = require("particlejs"); 

That goes in your pack file.这在你的包文件中。 Then you have particlejs available in your window object.然后您的 window 对象中有可用的particlejs。

You can also do:你也可以这样做:

// config/webpack/custom.js
module.exports = {
  resolve: {
    alias: {
      jquery: 'jquery/src/jquery',
      particle_js: 'particlejs'
    }
  }
}

Also try this:也试试这个:

import 'particles.js/particles';
const particlesJS = window.particlesJS;
particlesJS.load('particles-js', 'particles.json', null);

I ran into this exact same issue, here is what I did to get it working.我遇到了完全相同的问题,这是我为使其正常工作所做的工作。

If you're using yarn to install/add packages then run the following command from the root of your application to add the particles.js to the node_modules folder:如果您使用 yarn 安装/添加包,请从应用程序的根目录运行以下命令,将particles.js 添加到 node_modules 文件夹:

yarn add particles.js

Now open up app/javascript/packs/application.js and add the following:现在打开app/javascript/packs/application.js并添加以下内容:

require("particles.js")

Now restart your rails server and it should work.现在重新启动 Rails 服务器,它应该可以工作了。

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

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