在router.events中获取活动的路由数据

Pas*_*cal 6 typescript angular2-routing angular

如何获取订阅的router.events内部的活动路由数据(如params)?

我也没有想订阅this.route.params!

// Catch any events in a certain component, where I subscribe to it!
this.router.events.subscribe(event =>
{ 
// The event object has only the url (when is NavigationStart/End), there is nothing useful for me


});
Run Code Online (Sandbox Code Playgroud)

更新

我的问题有所不同,因为我不问如何对路由器事件做出反应!

我要求如何在router.events中获取激活的路由参数!

那肯定是有区别的!

Gün*_*uer 5

this.router.events.subscribe(event =>
{ 
  console.log(this.router.routerState.root);
  console.log(this.router.routerState.root.firstChild);
  console.log(this.router.routerState.root.firstChild && this.router.routerState.root.firstChild.firstChild);
});
Run Code Online (Sandbox Code Playgroud)

  • 你想要 2 个解决方案,这不公平:P 顺便说一句。我在看到你在另一个帖子中发布的内容之前就问过这个问题...... (2认同)
  • 我期望你的反应准确无误:P (2认同)

rob*_*ing 5

merge(of(this.route.snapshot.params), this.router.events.pipe(
      filter((event) => event instanceof NavigationEnd),
      map(() => this.route),
      map((route) => {
        while (route.firstChild) route = route.firstChild;
        return route;
      }),
      filter((route) => route.outlet === 'primary'),
      mergeMap(route => route.params),
    )).subscribe(p => {})
Run Code Online (Sandbox Code Playgroud)