[英]I can't figure out why Angular component class add implements onInit interface
When you create a component through the "ng g component ~" command, the component class automatically implements 'OnInit' interface.通过“ng g component ~”命令创建组件时,组件class会自动实现'OnInit'接口。
The following snippet shows how a component can implement this interface to define its own initialization method.下面的代码片段展示了一个组件如何实现这个接口来定义它自己的初始化方法。
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnInit {
ngOnInit() {
// ...
}
}
In the Angular documentation above, it says that the interface is meant to define its own initialization method,在上面的 Angular 文档中,它说接口是为了定义自己的初始化方法,
Even if you omit the 'implement OnInit' syntax, there is no problem in defining it through the ngOnInit () {} syntax.即使省略 'implement OnInit' 语法,通过 ngOnInit () {} 语法定义它也没有问题。
I am not sure why I have to write it.我不知道为什么我必须写它。
An interface (similar to java) is a contract between a component and an interface implementation.接口(类似于java)是组件和接口实现之间的契约。 Angular developer push to use
OnInit
like initialization block (and not the component constructor). Angular 开发人员推动使用
OnInit
之类的初始化块(而不是组件构造函数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.