Angular 2+ 组件很好地扩展了组件,但 tslint 抱怨继承的生命周期挂钩

Angular 2+ Component extends Component fine, but tslint complaining on inherited lifecycle hooks

我有一个自己清理订阅的抽象基础组件:

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(){}
}

现在我正在创建工作组件:

export class CategorySelectorComponent extends NeatComponent {

    public constructor() { super(); }
    
    public ngOnInit() {
      // some code
    }
}

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

[tslint] Implement lifecycle hook interface OnInit for method ngOnInit in class CategorySelectorComponent (use-life-cycle-interface)

我想你可以通过制作

来覆盖它

发件人:

"use-life-cycle-interface": true,

至:

"use-life-cycle-interface": false,

有关测试用例的更多详细信息,Check this out

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