简体   繁体   中英

Angular RouterLink Parameters - Activated Route not working

I am trying to get routerLink Parameter using ActivatedRoute.

constructor(private route: ActivatedRoute) { }

ngOnInit() {
  this.pageTitle = this.route.snapshot.params['title'];
}

It shows the parameter first time only. But not again when the parameter is changed.

Link to StackBlitz example.

When I click Search Page a it shows title is test . But when I click on Search Page b it still keeps on showing title is test even though the title is testing . However when I click Home and then Search Page b it shows the updated parameter as testing .

How do I fix the issue so that when click Search Page b after Search Page a it shows me the updated parameter.

The way you have it, it works one time and one time only. It won't work for your criteria. If you would like to react, you have to do it in an observable way.

Try:

this.route.params.subscribe(params => {
        this.pageTitle = params['title'];
      });

Everytime the params changes, you will get a notification and the subscription will kick in and change the pageTitle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM