You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Data object is missing in router 3.0.0-alpha #9390
Hi,
This is not a typical issue, but I am upgrading my exisiting solution to the new 3.0.0-alpha router. I discovered that the data object from the old router is gone. What is the alternative for us that used the data object property to have some additional info about the route?
If you still need data on your route config, it will cost you lots of headache ... Angular 2 is not as DRY as it should be at this moment. Looking forward to router-RC.
Hope this helps!
app.routes.ts
import{Route}from'@angular/router';// create our own custom RouteConfiginterfaceAppRouterConfigextendsRoute{data?: any;}// Add data which is optional - Here we add title of each route as an exampleexportconsttopRoutes: Array<AppRouterConfig>=[{path: 'home',component: HomeComponent},{path: 'about',component: AboutComponent,data: {title: 'About'}},{path: '**',component: UnfoundComponent,data: {title: 'Not Found (404)'}},];
app.component.ts
import{Title}from'@angular/platform-browser';import{Router,NavigationEnd}from'@angular/router';import{topRoutesasknownRoutes}from'./app.routes';
@Component({selector: 'app',// ... stuff})exportclassAppimplementsOnInit{constructor(privaterouter: Router,privatetitle: Title){}name: string='My Site';ngOnInit(){// change page title and scroll to top of the page on route changethis.router.events.subscribe(s=>{if(sinstanceofNavigationEnd){this.title.setTitle(this.getRouteTitle(s.url));window.scrollTo(0,0);}});}getRouteTitle(url: string): string{// go through the route config(s) and fish out the title if any - Todo: cache title for next time// Alternatively - we may be able to get each route config directly, perhaps from CanActivatevartitle: string=this.name;for(varitemofknownRoutes){if(url==`/${item.path}`&&'data'initem&&'title'initem.data){title=`${title} | ${item.data.title}`;break;}}returntitle;}}
@un33k - Your solution does work, but cannot be used it with child routes since Route declares children?: Route[] and Typescript does not allow it to be redeclared.
@brianchance - This should work with children too. Route needs to be extended as well.
Also - I won't call it a solution as it is just playing around at this time. I am sure NG2 team will take their sweet time and include all these goodies into the final Router.
Great .. now how do you access the data on a route NavigationEnd event? The idea (as per code above) is to set the page title based on the title key in the route data.
@zoechi (thx) : For a simple task of changing the page title, each routable component must have the above code.
If route change event provided an easy access to the route itself, then a shared service could have been put in place to simply swap the title and do other route specific tasks.
Something like this on the app.component:
exportclassApp{
....constructor(privaterouter: Router,private: title: Title){}ngOnInit(){this.router.events.subscribe(s=>{if(sinstanceofNavigationEnd){letd=s.route.data;// if this was possiblethis.title.setTitle(d.title);window.scrollTo(d.x,d.y);}});}
....}
The route data is immutable right? If so, do we need to subscribe to get it? A reference to it, or a copy of it would do!
Activity
choeller commentedon Jun 21, 2016
I created an issue in the router-repo some days ago https://github.com/angular/vladivostok/issues/43 I think currently there is no alternative.
vicb commentedon Jun 21, 2016
@vsavkin could you please take a look ? Do we plan to have an upgrade doc ?
choeller commentedon Jun 21, 2016
I think there is currently also no way to access the configuration of the currently active route, or am I missing something?
brianchance commentedon Jun 23, 2016
+1
@choeller - looks like you can access the current route config using (would prefer next.route or next.routeConfig)
un33k commentedon Jun 25, 2016
If you still need data on your route config, it will cost you lots of headache ... Angular 2 is not as DRY as it should be at this moment. Looking forward to router-RC.
Hope this helps!
app.routes.ts
app.component.ts
brianchance commentedon Jun 25, 2016
@un33k - Your solution does work, but cannot be used it with child routes since Route declares
children?: Route[]and Typescript does not allow it to be redeclared.un33k commentedon Jun 25, 2016
@brianchance - This should work with children too. Route needs to be extended as well.
Also - I won't call it a
solutionas it is just playing around at this time. I am sure NG2 team will take their sweet time and include all these goodies into the final Router.vsavkin commentedon Jun 27, 2016
implemented here: https://Igithub.com/angular/angular/pull/9619
un33k commentedon Jul 3, 2016
Great .. now how do you access the data on a route
NavigationEndevent? The idea (as per code above) is to set the page title based on thetitlekey in the routedata.zoechi commentedon Jul 3, 2016
@un33k
you can subscribe to
datalikeun33k commentedon Jul 3, 2016
@zoechi (thx) : For a simple task of changing the page title, each routable component must have the above code.
If route change event provided an easy access to the route itself, then a shared service could have been put in place to simply swap the title and do other route specific tasks.
Something like this on the app.component:
The route
datais immutable right? If so, do we need to subscribe to get it? A reference to it, or a copy of it would do!choeller commentedon Jul 3, 2016
@un33k I already opened issue #9662 for that. Meanwhile you can use the following as a workaround:
un33k commentedon Jul 3, 2016
@choeller works (thx). Hopefully, simple tasks like this would be provided out of the box.
angular-automatic-lock-bot commentedon Sep 9, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.