【问题标题】:Particle js background for angular project?角度项目的粒子js背景?
【发布时间】:2018-09-03 04:31:23
【问题描述】:

谁能解释如何为 Angular 6 项目添加粒子 js 背景? 我按照以下链接进行了一些教程。但它对我不起作用。 https://github.com/VincentGarreau/particles.js/

谢谢。

【问题讨论】:

  • 您好,请接受下面的答案,或者告诉我您是否在实施时遇到问题。干杯!!

标签: angularjs particles.js


【解决方案1】:

这就是我让它在我的 NG6 项目中工作的方式:

  1. 从 npm 安装particles.js:npm iparticle.js --save 或确保已安装。
  2. 在 angular.json 的脚本部分添加 node_modules/particles.js/particles.js
  3. 在您的组件中添加:declare varparticleJS:any; 在@component 之前
  4. 转到particles.js 并根据自己的喜好修改粒子,然后下载particlejs-config.json 文件
  5. 将该文件作为particles.json 存储在您的资产/数据文件夹中
  6. 在您的组件 html 模板中添加一个 id = "particles-js" 的 div
  7. 在您的组件 ngOnInit 中添加以下代码:

    particlesJS.load('particles-js', 'assets/data/particles.json', function() { console.log('回调 -particle.js 配置加载'); });

希望这会有所帮助!

编辑:添加代码

import { Component, OnInit } from '@angular/core';
import { ParticlesConfig } from './../../../../../assets/data/particles';

declare var particlesJS: any;

@Component({
  selector: 'app-heading',
  templateUrl: './heading.component.html',
  styleUrls: ['./heading.component.scss']
})
export class HeadingComponent implements OnInit {
    constructor() { }

    ngOnInit() {
        // https://vincentgarreau.com/particles.js/
        particlesJS('particles-js', ParticlesConfig, function() {
            console.log('callback - particles.js config loaded');
          });
    }
}

模板

<div class="h-75 bg header">
    <div  id="particles-js" class="w-100 header-particles"  >

    </div>
    <div class="header-container w-100">
        <div>
            <h1> Big Header</h1>
            <div>small header</div>
        </div>
    </div>
</div>

以及在其他组件中的使用

<app-heading></app-heading>
<main>
  <app-features></app-features>
  <app-pricing-tier></app-pricing-tier>
  <app-testimonials></app-testimonials>
  <app-trusted-by></app-trusted-by>
</main>

【讨论】:

  • 确实做到了这一点并收到错误“无法在 window.particlesJS 中读取 null 的属性 'getElementsByClassName'”知道修复方法吗?
  • 只需要在 Angular 7 中设置相同的东西。必须在 AfterViewInit 而不是 OnInit 中初始化它以避免 getElementsByClassName 错误。
  • @codephobia 很奇怪。我在一个新项目中使用 angular 7.2.3 并将其放入 ngOnInit 没有问题
  • @codephobia。您是否偶然在 ngFor 或其他可能会延迟创建元素的构造中创建组件?
  • @AlbertoL.Bonfiglio 你能告诉我,如何正确地将其包含在我的测试中吗?由于ReferenceError: particlesJS is not defined,基本的should create 测试不起作用。我正在 Karama 进行测试,并且还尝试通过将 particlesJS 变量的声明外包到 typings.d.ts 并将其作为 files:['typing.d.ts'] 包含在我的 karma.conf.ts 中(服务正常,但不能测试)
【解决方案2】:

我想在Alberto 的回答中添加更多内容。我在 Angular CLI 8.3.2 版上,一切正常。正如问题所问,我实际上想将它添加到我的组件的背景中。我使用这样的 CSS 实现了这一点。

HTML

<div id="container">
  <div id="particles-js">
  </div>
  <div id="over">
      <!--Existing markup-->
  </div>
</div>

CSS

#container {
  width: 100px;
  height: 100px;
  position: relative;
}
#particles-js,
#over {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
#over {
  z-index: 10;
}

此设置将在您现有的标记下应用particles.js 背景。

编辑:

如果您在 Windows (IIS) 上使用 Azure 应用服务将其部署到生产环境,您可能会收到 particles.json 的 404 not found 错误。在这种情况下,在src 文件夹中创建一个web.config 文件,如下所示,并将其包含在angular.jsonassets 数组中

web.config

<configuration>
  <system.webServer>
    <staticContent>
        <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
    <rewrite>
      <rules>
        <rule name="Angular" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

angular.json

"assets": [
     "projects/dashboard/src/favicon.ico",
     "projects/dashboard/src/assets",
     "projects/dashboard/src/web.config"
]

【讨论】:

【解决方案3】:

您可以使用“angular-particle”轻松实现粒子动画,它是使用 TypeScript for Angular 实现的particle.js

它的实现非常简单,您可以在下面的链接中找到它 https://www.npmjs.com/package/angular-particle

已编辑

这是 angular 8 中 angular-particle 的运行示例 https://github.com/SunnyMakode/angular-particle-demo

从 github 拉取代码后,

  1. 键入“npm install --save”并从终端窗口回车
  2. 输入“ng serve”并回车

【讨论】:

  • "angular-particle" 对我来说效果很好。顺便说一句,我正在使用 Angular 8。我会尽快发布github链接以供参考
  • 嗨蒂莫西,这是 angular 8 github.com/SunnyMakode/angular-particle-demo 中 angular-particle 的运行示例从 github 中提取代码后,>> 键入“npm install --save”并从终端窗口按 Enter >> 输入“ng serve”并回车
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
相关资源
最近更新 更多