ES5中的Angular 2路由?

时间:2016-01-05 04:30:24

标签: angular angular2-routing

Angular 2 ES5 cheat sheet说要这样做:

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方法。你是如何设置的?

1 个答案:

答案 0 :(得分:1)

这是我设法最终开始工作的一种可能方式。我愿意向其他人发布更好的解决方案:

app.AppComponent = ng.core
    .Class({
        constructor: [
            function() {
            }
        ]
    });

app.AppComponent.annotations = [
    ng.router.RouteConfig([
        { path: '/', component:app.ListsComponent, name:'Lists' },
        { path: '/children', component:app.ChildrenComponent, name:'Children' }
    ]).annotations[0],

    new ng.core.ComponentMetadata({
      selector: 'the-app',
      template: '<h1>App!!!</h1>' +
        '<a [routerLink]="[\'Children\']">Children</a>' +
        '<a [routerLink]="[\'Lists\']">Lists</a>' +
        '<router-outlet></router-outlet>',
      directives:[
          app.ListsComponent,
          app.ChildrenComponent,
          ng.router.ROUTER_DIRECTIVES
      ]
  })
];