Angular2 router - Navigate to the current page with different parameters

前端 未结 4 1980
梦毁少年i
梦毁少年i 2020-12-30 23:56

I am trying to route to the current page with different param with no avail. I have a combo box that triggers:

this.router.navigate([\'page\', selectedId]);
         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-31 00:41

    The page will not refresh, cause that's how the Router works.. without refreshing the page!

    You have to subscribe to the route parameters to detect changes in them and then do whatever you want to with that information..

    So inject ActivatedRoute into your component and subscribe to the parameters like this:

    constructor(private route: ActivatedRoute) {}
    
    ngOnInit() {
      this.route.params.subscribe(params => {
         // PARAMS CHANGED .. TO SOMETHING REALLY COOL HERE ..
    
         // for example extract the id..
         let id = +params['id']; // (+) converts string 'id' to a number
    
       });
    }
    

提交回复
热议问题