在Angular 2中使用生命周期接口

时间:2016-08-12 12:28:59

标签: angular typescript tslint

这条规则究竟实现了什么?

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

2 个答案:

答案 0 :(得分:12)

它只是意味着你必须为你使用的每个生命周期钩子添加implements关键字,

虽然角度识别和使用挂钩不是必需的,但是对于代码清晰度和维护来说要好得多。

示例:

// 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...
  }
}

答案 1 :(得分:3)

它不是内置的tslint规则。它是由codelyzer定义的规则。

GitHub回购有一个视频(我没看过),但文档很少。幸运的是,作者已经实施了测试,因此可以从use-life-cycle-interface rule推断出test descriptions的作用:

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`, ...