Angular2 RC4 Router: Get RouteConfigs -Property Data from a Router Event Subscription in a Service

With the new RC4-Router (3.0.0-beta.2) from Angular2, I have the following Route-Setup:

export const Routes: RouterConfig = [
    {
        path: 'account',
        pathMatch: 'prefix',
        canActivate: [
            AppAuthGuard
        ],
        children: [
            {
                path: 'create',
                component: AccountRegistration,
                data: {
                    'title': 'Create an Account'
                }
            },
            {
                path: 'login',
                component: AccountLogin,
                data: {
                    'title': 'Log into your Account'
                }
            }
        ]
    }
];

You can open routes. But I have a global service, which should set the title in the AppComponent from the "title" - properties of the properties of the "data" routes. So I sign router.events-Observable:

this.router.events.subscribe(value => {

    if (value instanceof NavigationEnd) {

       console.log(this.router.routerState.snapshot.root.data['title']);
    }
}

But getting the title is simply impossible. Is this possible, and should I be able to get route information? Does anyone have an example for me?

+4
source share
1 answer

You can sign up for datajust like

class HeroDetailComponent {
  ngOnInit() {
    this.sub = this.route
      .data
      .subscribe(v => console.log(v));
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
} 
+1

All Articles