-2

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Annas
  • 35
  • 4
  • Read the docs? E.g. `OnInit` is a ([technically optional](https://angular.io/guide/lifecycle-hooks#interfaces-are-optional-technically)) interface for one of Angular's lifecycle hooks. – jonrsharpe Nov 04 '19 at 07:59

1 Answers1

1

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

Daniel
  • 951
  • 7
  • 20