Angular2 Get router params outside of router-outlet

后端 未结 5 1320
不思量自难忘°
不思量自难忘° 2021-01-04 14:26

I have a dashboard application which consists of a treeview component (which lists various content nodes) and a dashboard-edit component which renders some editable content

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 14:58

    This solution worked for me: complete example.

    constructor(
      private readonly router: Router,
      private readonly rootRoute: ActivatedRoute,
    ){
      router.events.pipe(
        filter(e => e instanceof NavigationEnd),
        map(e => this.getParams(this.rootRoute))
      ).subscribe(params => {
       //
      });
    }
    
    private getParams(route: ActivatedRoute): Params {
      // route param names (eg /a/:personId) must be ditinct within
      // a route otherwise they'll be overwritten
      let params = route.snapshot.params
      params = { ...route.snapshot.queryParams, ...params}
      if(route.children){
        for(let r of route.children){
          params = {...this.getParams(r), ...params};        
        }
      }
      return params;
    }
    

    Credits to Toxicable.

提交回复
热议问题