0

I need to pass a property from my component to the RouteData class.

@Component({
    selector: 'profile',
    templateUrl: 'profile.html',
})

@RouteConfig([
  {path:'/posts', name: 'ProfilePosts',   component: ProfileTracksComponent, data: {user}, useAsDefault: true}
])

export class ProfileComponent implements OnInit {
    user = {...}

I am trying to get the user property into the RouteData (see the data property in RouteConfig), however angular2 says that user is not defined. this.user does not work either.

How can I do this?

Sebastian Olsen
  • 10,318
  • 9
  • 46
  • 91
  • 1
    If there was a solution Günter would have given it [here](http://stackoverflow.com/questions/36108771/can-routedata-in-angular-2-pass-variables-to-routed-components-from-parent-compo). But he got downvoted instead. For now you can use a global service as he also suggested. – Ankit Singh Apr 27 '16 at 04:59

1 Answers1

0

You can't reference a component instance from a decorator applied to the class.
You can either use a shared service inject the Router and call one of it's router.navigateXxx() functions where you pass additional data.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • There has to be a better way, in my situation the user is different based on the id in the url. So using a shared service would be difficult, why is something like this that should be so easy, almost impossible? – Sebastian Olsen Apr 27 '16 at 11:58
  • Almost impossible is quite some exxageration. If you implement [CanReuse](https://angular.io/docs/ts/latest/api/router/CanReuse-interface.html) `router.navigate()` is quite cheap. I also don't see why a service would be difficult. Also the router is still work-in-progress. – Günter Zöchbauer Apr 27 '16 at 12:01