2

Angular 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方法。你如何设置这个?

4

2 回答 2

6

我已经完成了以下似乎运作良好的方式。

app.AppComponent = ng.core
    .Component({
       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
       ]
    })
    .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.Componentng.router.RouteConfig都是装饰器,您可以编写:

app.AppComponent = ng.core.Class(...);
app.AppComponent = ng.core.Component(...)(app.AppComponent);
app.AppComponent = ng.router.RouteConfig(...)(app.AppComponent);

希望有帮助。

于 2016-01-07T04:02:02.500 回答
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
      ]
  })
];
于 2016-01-05T05:11:55.223 回答