简体   繁体   中英

Use Lifecycle Interface in Angular 2

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

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

it simply means that you have to add the implements keyword for every lifecycle hook you use,

while this is not necessary for angular to recognize and use the hooks, it is much better for code clarity and maintenance.

example:

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

It's not a built-in tslint rule. It's a rule that's defined by codelyzer .

The GitHub repo has a video (that I've not watched), but little documentation. Fortunately, the author has implemented tests, so it's possible to infer what the use-life-cycle-interface rule does from its 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`, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM