这条规则的具体作用是什么?
"use-life-cycle-interface": true,
它只是意味着您必须为您使用的每个生命周期挂钩添加 implements 关键字,
虽然这对于 Angular 识别和使用钩子来说不是必需的,但它对于代码的清晰性和维护性要好得多。
例子:
// don't forget the import
import { AfterViewInit } from '@angular/core';
// you have to have this implements statement
export class YourComponent implements AfterViewInit {
// if you want to use this hook
ngAfterViewInit() {
// your code...
}
}
这不是一个内置tslint
规则。这是codelyzer定义的规则。
GitHub repo 有一个视频(我没有看过),但文档很少。幸运的是,作者已经实现了测试,因此可以从测试描述中推断出use-life-cycle-interface
规则的作用:
it(`should fail, when a life cycle hook is used without implementing it's interface`, ...
it(`should fail, when life cycle hooks are used without implementing their interfaces`, ...
it(`should fail, when some of the life cycle hooks are used without implementing their interfaces`, ...