为什么导入OnInit很重要

Ash*_*Jha 2 angular

无需导入OnInit和实现

ngOnInit(){
  this.recipies=this.recipeService.getRecipies();
} 
Run Code Online (Sandbox Code Playgroud)

这仍然工作正常没有任何抱怨所以我为什么要使用像:

import { Component,EventEmitter,Output , OnInit } from '@angular/core';

export class RecipiesListComponent implements OnInit{}
Run Code Online (Sandbox Code Playgroud)

ran*_*al9 6

接口是可选的,因为每角文档,这样你就可以在不使用它importingimplementing它.

Angular会检查指令和组件类,并在定义它们时调用钩子方法,但最好将接口添加到TypeScript指令类,以便从强类型和编辑器工具中受益.