2

伙计们。又是一次愚蠢的问题。

在学习使用 node 和 express 时,我正在研究使用particles.js作为背景。在 github 上提供的文档中,它说要使用:

npm install particles.js

这样做之后,我添加了这个以在我的app.js中调用它:

var particleJS = require("particle.js")

然而,此时,当我尝试运行我的应用程序时,节点错误状态:

ReferenceError:未定义窗口

还有另一个代码块应该被调用来创建这个应该在前端运行的代码块,使用玉文件。

/* particlesJS.load(@dom-id, @path-json, @callback (optional)); */
particlesJS.load('particles-js', 'assets/particles.json', function() {
console.log('callback - particles.js config loaded');
});

有几件事我不太确定。首先,从我对 node 的乐趣来看,应该可以在 app.js 中调用npm install模块。所以我觉得它不起作用很奇怪..这是因为有问题的代码不是基于使用像 express 这样的框架吗?

其次,如果是这种情况并且我已经通过 npm 安装了particles.js 模块,为了让jade 能够拾取它,我必须将js 复制到“javascript”公共目录以便它可以找到它?

如果是这样的话,我首先不需要通过 npm 安装它?

这么多问题......对不起。我还在学习,并且在这个方面有点跑来跑去。也许我的逻辑在某个地方是错误的。

任何人都可以帮忙吗?

4

2 回答 2

1

所以你走在正确的轨道上,当你 npm install 一个前端库时,你需要使用另一个库将它加载到视图中。我猜他们假设您正在使用这样的框架,或者将其放入前端框架的 app.js(react 等)中。我只需下载库并将其放入您的静态文件夹并以这种方式运行。

于 2018-01-06T14:19:00.353 回答
0

我更改了脚本并引用了我在外面创建的文件夹的档案particles.js 和particles.json

HTML

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Menu</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

    <style>
        body{
            background-color: lightgrey;
        }
        *{
            padding : 0px;
            margin : 0px;
            font-family: Century Gothic, CenturyGothic, AppleGothic, sans-serif;
        }
      </style>

<!--<div id="particles-js"></div>-->
<div id="particles-js" style="position : absolute; width:100%; height:100%; z-index:-1; background-color: #000000"></div>

  <div class="container">
      <img src="../logo.jpg" width="600" class="img-fluid wall" style="display: block; margin: 0 auto">
  </div>

  <div class="container">
      <Form action = "/game" method = "POST">
        <button id="Iniciar_juego" type="submit" class="btn btn-primary btn-lg">Iniciar juego</button>
        <Input name="textbox" id="usuario" class="form-control" aria-label="Nombre de usuario:" aria-describedby="inputGroup-sizing-default" type="text" value="${salida}"style="display: none">
      </Form>
      <Form action = "/tabla" method = "GET">
        <button id="Ranking" type="submit" class="btn btn-primary btn-lg">Ver Ranking</button>
      </Form>
      <Form action = "/inst" method = "GET">
        <button id="Instrucciones" type="submit" class="btn btn-primary btn-lg">Instrucciones</button>
      </Form>
      <Form action = "/" method = "GET">
        <button id="Salir" type="submit" class="btn btn-primary btn-lg">Salir</button>
      </Form>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/particles.js@2.0.0/particles.min.js"></script>

  <script>
    particlesJS.load('particles-js','../particles.json',function(){
      console.log('particles.json loaded...');
    });
  </script>
</body>
</html>
于 2020-06-15T02:01:02.280 回答