【发布时间】:2018-09-03 04:31:23
【问题描述】:
谁能解释如何为 Angular 6 项目添加粒子 js 背景? 我按照以下链接进行了一些教程。但它对我不起作用。 https://github.com/VincentGarreau/particles.js/
谢谢。
【问题讨论】:
-
您好,请接受下面的答案,或者告诉我您是否在实施时遇到问题。干杯!!
谁能解释如何为 Angular 6 项目添加粒子 js 背景? 我按照以下链接进行了一些教程。但它对我不起作用。 https://github.com/VincentGarreau/particles.js/
谢谢。
【问题讨论】:
这就是我让它在我的 NG6 项目中工作的方式:
在您的组件 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>
【讨论】:
ReferenceError: particlesJS is not defined,基本的should create 测试不起作用。我正在 Karama 进行测试,并且还尝试通过将 particlesJS 变量的声明外包到 typings.d.ts 并将其作为 files:['typing.d.ts'] 包含在我的 karma.conf.ts 中(服务正常,但不能测试)
我想在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.json 的assets 数组中
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"
]
【讨论】:
您可以使用“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 拉取代码后,
【讨论】: