How to use RouteData in Angular2 beta

I encounter problems when I try to use RouteData in RouteData beta using TypeScript.

I insert it into the constructor and import it correctly

 import {RouteConfig, Router, RouteData} from 'angular2/router'; export class App { constructor(public router: Router, public data: RouteData) { // router works - routedata not } } 

I get No provider for RouteData! (App -> RouteData) No provider for RouteData! (App -> RouteData) .

If I include it in a component annotation like this

 @Component({ //.. providers: [RouteData] }) 

I get this error: Cannot resolve all parameters for RouteData(?). Make sure they all have valid type or annotations. Cannot resolve all parameters for RouteData(?). Make sure they all have valid type or annotations.

+5
source share
1 answer

RouteData provides data to your child component using RouteConfig in your parent component. There should not be a need to use it in an AppComponent.

To use it, you must specify RouteConfig in your AppComponent as follows:

 @RouteConfig([ {path: '/child', name: 'Child', component: ChildCmp, data: {item: 'hi there'}} ]) 

Your ChildComponent should then enter RouteData and be able to get a set of parameters on the route as follows:

 export class ChildCmp { constructor(@Inject(RouteData) private data:RouteData) { this.data.get("item") } } 
+5
source

Source: https://habr.com/ru/post/1241426/


All Articles