嘿,大家好,我收到了一个皮棉错误,我不确定它在说什么,这是错误: src/app/particles/particles.component.ts[4, 1]: Implement lifecycle hook interfaces (https://angular.io/docs/ts/latest/guide/style-guide.html#!#09-01)行nr 4是@Component({

我已经阅读了它提供的链接,我得到了它试图告诉我的东西(至少我认为是这样的:),但我看不出它在这种情况下是如何应用的.

谢谢你的帮助.

import { Component, ViewChild, ElementRef, HostListener} from '@angular/core';
import { Particle } from './particle';

@Component({
  selector: 'km-particles',
  styles: ['canvas { transition: opacity 0.5s cubic-bezier(0.4, 0.0, 0.2, 1);}'],
  template: ` <canvas #canvas
              [attr.width]='width'
              [attr.height]='height'
              [style.opacity]='opacity'>
              </canvas>`
})
export class ParticlesComponent {

  private ctx: CanvasRenderingContext2D;
  private width: number;
  private height: number;
  private opacity: number;
  private particles: Particle[];
  private particleClearLoop: any;

  public playParticles: boolean;

  // get the element with the #canvas on it
  @ViewChild('canvas') canvas: ElementRef;

  // on window resize, restart the animation with the new boundaries
  @HostListener('window:resize', ['$event'])
  OnResize(event: Event) {
    this.initAnim();
  }

  constructor() {
    this.opacity = 0;
  }

  // wait for the view to init before using the element
  ngAfterViewInit() {
    this.ctx = this.canvas.nativeElement.getContext('2d');

    // ok all is ready, start the particle animation
    this.initAnim();
  }

  createParticles() {
    this.particles = [];
    // let's make us some particles
    for (let i = 0; i < 150; i++) {
      this.particles.push(new Particle());
    }
  }

  draw() {
    // clear canvas
    this.ctx.clearRect(0, 0, this.width, this.height);

    // draw the particles
    this.particles.forEach((particle) => {

      particle.timestamp = Math.floor(Date.now());
      particle.counter = particle.sign * (particle.timestamp / particle.speed * Math.PI);

      this.ctx.beginPath();
      // define the circleparticle
      this.ctx.arc( particle.xPos + particle.radius * Math.cos(particle.counter / 100),
                    particle.yPos + particle.radius * Math.sin(particle.counter / 100),
                    particle.width,
                    0,
                    Math.PI * 2,
                    false);

      this.ctx.globalAlpha = particle.alpha;
      this.ctx.fillStyle = particle.color;
      this.ctx.fill();

    });

    if (this.playParticles) {
      requestAnimationFrame(this.draw.bind(this)); // AGAIN!
    }

  }

  // init resize and make the particles
  initAnim() {
    this.playParticles = false;
    this.opacity = 0; // so we don't see the flicker

    clearInterval(this.particleClearLoop);

    this.particleClearLoop = setTimeout(() => {
      this.opacity = 1;
      this.playParticles = true;
      this.resize();
      this.createParticles();
      this.draw();
    }, 500);
  }

  // method that resizes the canvas to the full width/height of the window
  resize() {
    this.width = window.innerWidth;
    this.height = window.innerHeight;
  }
}

推荐答案

您使用的是ngAfterViewInit Lifecycle Hook,您只需添加以下内容即可让TSLint满意,

export class ParticlesComponent implements AfterViewInit

希望这有帮助!!

Angular相关问答推荐

如何在Angular 17中使用Angular—calendum?

Angularmaterial 中的处理mat—radio—button表单值访问器

Swiper 11角17 SSR Swiper断点不工作

对子router-outlet 应用主机样式

Angular 环境本地无文件替换

带信号数据源的17角material 表

选中/取消选中-复选框未正确返回TRUE或FALSE

懒惰加载角404路径

Angular 16模块中未使用的组件是否从Bundle 包中删除(树摇动)?

找不到模块webpack dev服务器

Angular-每2个流式传输多个HTTP调用

我的验证器收到一个始终为空的可观察值

http 调用的 RxJs 数组

PrimeNG 避免在同一位置使用不同键的 cogo toast 重叠

如何在 Angular 中创建通用的可重用组件

Angular keypress 存储空间而不是第一个字母

Angular 7 测试:NullInjectorError:No provider for ActivatedRoute

CORS 错误:requests are only supported for protocol schemes: http…等

NG2-Charts 无法绑定到 datasets,因为它不是 canvas的已知属性

为什么 ngOnInit 被调用两次?