Angular 2+ Component 可以很好地扩展 Component,但 tslint 抱怨继承的生命周期钩子

Ant*_*gov 5 tslint angular

我有一个抽象的基本组件,可以自己清理订阅:

import { OnDestroy, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';

export abstract class NeatComponent implements OnDestroy, OnInit {
// Add '.takeUntil(this.ngUnsubscribe)' before every '.subscrybe(...)'
// and this subscriptions will be cleaned up on component destroy.

  protected ngUnsubscribe: Subject<any> = new Subject();

  public ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

  public ngOnInit(){}
}
Run Code Online (Sandbox Code Playgroud)

现在我创建工作组件:

export class CategorySelectorComponent extends NeatComponent {

    public constructor() { super(); }
    
    public ngOnInit() {
      // some code
    }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但 tsLint 不喜欢我的 ngOnInit 方法:

[tslint] 为 CategorySelectorComponent 类中的 ngOnInit 方法实现生命周期钩子接口 OnInit(使用生命周期接口)

Ben*_*din 6

我对没有继承的组件也有同样的错误,我通过添加实现 OnDestroy 来纠正它。


Viv*_*shi 4

我认为你可以通过以下方式覆盖这个

从 :

"use-life-cycle-interface": true,
Run Code Online (Sandbox Code Playgroud)

到 :

"use-life-cycle-interface": false,
Run Code Online (Sandbox Code Playgroud)

有关测试用例的更多详细信息,请查看此