该角2 ES5小抄说,要做到这一点:
var MyComponent = ng.router.RouteConfig([ { path: '/:myParam', component: MyComponent, as: 'MyCmp' }, { path: '/staticPath', component: ..., as: ...}, { path: '/*wildCardParam', component: ..., as: ...} ]).Class({ constructor: function() {} });
但是,我无法弄清楚如何指定@Component
该类的东西,以便我可以实际实例化它.例如,
ng.router.RouteConfig([...]).Component({})
抛出异常,因为结果.RouteConfig
没有.Component
方法.同样,结果.Component
没有.RouteConfig
方法.你如何设置这个?
我做了以下似乎运作良好的方式.
app.AppComponent = ng.core .Component({ selector: 'the-app', template: `App!!!
Children Lists`, directives:[ app.ListsComponent, app.ChildrenComponent, ng.router.ROUTER_DIRECTIVES ] }) .Class({ constructor: [ng.router.Route, function(_router) { this._router = _router; // use for navigation, etc }] }); app.AppComponent = ng.router .RouteConfig([ { path: '/', component:app.ListsComponent, name:'Lists' }, { path: '/children', component:app.ChildrenComponent, name:'Children' } ])(app.AppComponent);
由于这两个ng.core.Component
和ng.router.RouteConfig
的装饰,你可以这样写:
app.AppComponent = ng.core.Class(...); app.AppComponent = ng.core.Component(...)(app.AppComponent); app.AppComponent = ng.router.RouteConfig(...)(app.AppComponent);
希望有所帮助.