简体   繁体   English

在 Angular 6 中的 class 上实现

[英]Implements on a class in Angular 6

How can we get to know when we have to use implements in Angular?我们如何知道何时必须使用implements中的工具? I don't understand this when we implement a class and how we know that this class will be implements on this class.当我们实现 class 以及我们如何知道这个 class 将在这个 class 上实现时,我不明白这一点。

Just like we use in our components:就像我们在组件中使用的一样:

implements OnInit, DeletePopup

and some other interfaces also so why we use this and secondly how we get to know we have to use now OnInit or some other interface?以及其他一些接口,为什么我们要使用它,其次我们如何知道我们现在必须使用OnInit或其他一些接口?

You will have to implement a class when you want to modify the behavior or add functionality to a component in certain cases.在某些情况下,当您想要修改行为或向组件添加功能时,您必须实现 class。

The implementation of life cycles such as OnInit , implies that you must create a method called ngOnInit in your component OnInit等生命周期的实现意味着您必须在组件中创建一个名为ngOnInit的方法

export declare interface OnInit {
    ngOnInit(): void;
}

In this case, ngOnInit is used to initialize its angular component, see: Angular 2 Component Constructor Vs OnInit在这种情况下,ngOnInit 用于初始化其 angular 组件,参见: Angular 2 Component Constructor Vs OnInit

In terms of life cycles you have all these options: https://angular.io/guide/lifecycle-hooks#lifecycle-sequence在生命周期方面,您有所有这些选项: https://angular.io/guide/lifecycle-hooks#lifecycle-sequence

To know if you must implement an interface you will have to read the documentation of the functionality you are trying to add, In the case of life cycles, implementing them is a possibility, not a requirement: https://angular.io/guide/lifecycle-hooks#interfaces-are-optional-technically要知道您是否必须实现一个接口,您必须阅读您尝试添加的功能的文档,在生命周期的情况下,实现它们是可能的,而不是要求: https://angular.io/guide /lifecycle-hooks#interfaces-are-optional-technically

Angular offers lifecycle hooks that provide visibility into these key life moments and the ability to act when they occur. Angular 提供了生命周期挂钩,可提供对这些关键生命时刻的可见性以及在它们发生时采取行动的能力。 https://angular.io/guide/lifecycle-hooks https://angular.io/guide/lifecycle-hooks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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