简体   繁体   中英

Implements on a class in Angular 6

How can we get to know when we have to use implements in Angular? I don't understand this when we implement a class and how we know that this class will be implements on this 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?

You will have to implement a class when you want to modify the behavior or add functionality to a component in certain cases.

The implementation of life cycles such as OnInit , implies that you must create a method called ngOnInit in your component

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

In this case, ngOnInit is used to initialize its angular component, see: Angular 2 Component Constructor Vs OnInit

In terms of life cycles you have all these options: 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

Angular offers lifecycle hooks that provide visibility into these key life moments and the ability to act when they occur. https://angular.io/guide/lifecycle-hooks

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