Ngoninit Called Everytime I Change Route
I have a controller implements OnInit The problem here is whenever i change the route and come back to same component ngOnInit is called everytime. What i am doing wrong i am not
Solution 1:
ngOnInit()
is executed everytime the component is loaded. It doesn't need to be called. This is a lifecycle hook for doing initial stuff. You can learn more about angular lifecycle hooks
here
Solution 2:
If in the constructor you subscribe to the active route, ngInit will be called every time the router navigates to that page.
constructor(private route: ActivatedRoute,
private router: Router
) {
this.route.queryParams.subscribe(async (params) => {
if (this.router.getCurrentNavigation().extras.state) {
// TODO save the params
}
});
}
ngOnInit(){
console.log('ngOnInit called');
}
Post a Comment for "Ngoninit Called Everytime I Change Route"