Angular 2: Route parameter changes, reload 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()
})
}
}
Params is an Observable. So you only have to subscribe once to the observable and everytime the parameters change, you get notified.
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.params
.switchMap((params: Params) => /* load your user e.g. getUser(params['id']) */)
.subscribe(user => this.user = user);
}
You also need to import ActivatedRoute and Params
import { ActivatedRoute, Params } from '@angular/router';