How to get current route custom data in angular 2?

后端 未结 11 1855
旧时难觅i
旧时难觅i 2020-12-05 06:21

I have set up routes is like below

const appRoutes: Routes = [
  {
    path: \'login\',
    component: LoginComponent,
    data: {
      title: \'Login TTX\         


        
11条回答
  •  生来不讨喜
    2020-12-05 07:13

    After I tried a lot, I found the best solution

      private ngUnsubscribe$ = new Subject();
    
        constructor (private router: Router, private route: ActivatedRoute) { }
        
         ngOnInit () {
            const routeEndEvent$ = this.router.events
              .pipe(
                filter(e => e instanceof NavigationEnd),
                tap(() => console.warn("END")),
            );
        
            this.router.events
              .pipe(
                filter(e => e instanceof ChildActivationEnd && e.snapshot.component === this.route.component),
                buffer(routeEndEvent$),
                map(([ev]) => (ev as ChildActivationEnd).snapshot.firstChild.data),
                takeUntil(this.ngUnsubscribe$)
              )
              .subscribe(childRoute => {
                console.log('childRoute', childRoute);
              })
          }
    

提交回复
热议问题