I have a route with a parameter, when going to the page /users/123 where 123 is the parameter the ngOnInit is called, I get my parameter and I call my method to get the user.
However, when I'm on that page and I click a second link /users/456 then the ngOnInit is not called anymore (because that page is already instantiated). So without ngOnInit I can't get the route parameter and can't call my method to get the user.
If I go to /users/123, then to /home, then to /users/456, it works obviously.
What do I have to use to make sure the function that gets the parameter and gets the user is ALWAYS called even if I'm already on the same page?
You can use subscriber route.params
to listen the param's change.
import { ActivatedRoute } from '@angular/router';
export class DemoPage {
constructor(private _route: ActivatedRoute) { }
ngOnInit() {
this._route.params.forEach(params => {
let userId = params["userId"];
//call your function, like getUserInfo()
})
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With