ES5 中的 Angular 2 路由?

标签 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 方法。如何进行此设置?

最佳答案

我已经完成了以下方法,看起来效果很好。

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);

希望有帮助。

关于ES5 中的 Angular 2 路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34604160/

相关文章:

Angular 2 : Inject a dependency into @CanActivate?

angular - 我如何导航到兄弟路线?

cordova - 直接从 ionic2 typescript 组件访问 Cordova 插件

angular - 检测 @ViewChild() 中的值何时更改

javascript - 更改事件未被识别

angular - 如何以 Angular Testing routerLinkActive

css - Angular 2没有为选择箭头div生成唯一的组件标识符

angular - 使用 Angular2 应用 MVVM

Angular2在路由不活动时添加类

javascript - Angular 功能路由模块 - 子组件未加载