In angular 6 when we create a component the the .ts file of that component has a class which implements the OnInit class but when we remove the default method(ngOnInit), it works fine. so my question is if it implements some class then it must had some method which had to be implemented in this implemented class, if this is not necessary to have methods then what is the purpose to implement OnInit class.
ngOnInit
is a life cycle hook called by Angular to indicate that Angular is done creating the component.
We import OnInit
in order to use it, implementing OnInit
is not mandatory but considered good practice):
import {Component, OnInit} from '@angular/core';
We use ngOnInit to do something after the component has been created. for example you can set variables here, call methods etc.
https://angular.io/api/core/OnInit
If you import OnInit
and do export class AppComponent implements OnInit
then you have to add the ngOnInit()
method otherwise you will have a compilation error
OnInit
and do export class AppComponent implements OnInit
then you have to add the ngOnInit()
method –
Elemi OnInit is not a class but an interface provided by the framework so that if you want to do something when a component is initialized you can provide an ngOnInit() method in your class and have your code there. More on ngOnInt: https://angular.io/api/core/OnInit
If you have implemented OnInit interface but have not provided the ngOnInit you will get a compilation error, in your code thrown by VSCode or some other editor.
Something like below:
Class 'ClaimComponent' incorrectly implements interface 'OnInit'. Property 'ngOnInit' is missing in type 'GiftingClaimComponent'.
However it will run fine as eventually the TS code compiles down to JavaScript.
And since JavaScript has no such type safety or the concept of interfaces that we get in TypeScript. However if you try to create an AOT build of your application it will fail and you will have to provide the implementation.
As @Patricio Vargas say, if u export your class component that implements the OnInit
interface, you'll need to implement the ngOnInit()
method, otherwise you'll get error TS2420
(You can try of course on stackblitz by simply implementing the OnInit interface inside the default AppComponent provided for Angular projects.
Regarding the second point, the purpose of implementing that interface, you should keep in mind the 2 phase of Angular bootstrap:
- Component tree construction
- Changes detection
It's a good practice (sometimes needed) to initialize your component using the OnInit
hook and not inside the constructor (that instead must be used for DI), mostly because the constructor invocation is part of the 'Angular component tree construction', and then input bindings will not be available inside the constructor (since they are part of the detection change phase.
© 2022 - 2024 — McMap. All rights reserved.
OnInit
, it has to include anngOnInit
method, otherwise there will be compilation errors (although the application will still work, which is what OP said in the question). JBNizet is absolutely correct about that. – Racecourse