angular - 新的 Angular2 路由器配置

标签 angular angular2-routing

在使用已弃用的路由器时,我能够执行 router.config 并传入一个对象。问题是,路由器本身在应用程序启动后进行了一些配置,该对象具有与我使用 @RouterConfig 相同的"template"。我正在寻找的是是否有一种方法可以像这样配置新路由器。一直在查看文档,但我有点不知所措,因为它还没有记录在案。

根据回答进行编辑

不,我不能使用@Routes。事情是我在构建路由器后加载配置。这是我如何使用旧路由器进行操作的片段:

       myLoader.loadComponentConfig(configPath)
      .then(components => { self.Components = components;
         components.map(comp => {
             self.RouterComponents.push(
                 {
                     path: '/' + comp.name,
                     component: comp,
                     as: comp.name
                 }
             )});
             router.config(self.RouterComponents);
        });

如您所见,我正在为自己构建一个 json 对象 ( RouterComponents ),然后将其发送到路由器。我正在寻找一种方法来对新路由器或类似的东西执行相同的操作。

最佳答案

在新路由器中(>= RC.3)https://angular.io/docs/ts/latest/api/router/index/Router-class.html resetConfig可以使用

router.resetConfig([
 { path: 'team/:id', component: TeamCmp, children: [
   { path: 'simple', component: SimpleCmp },
   { path: 'user/:name', component: UserCmp }
 ] }
]);

您可能需要提供一些虚拟路由器配置,以免在应用程序启动时出现错误。

https://github.com/angular/angular/issues/11437#issuecomment-245995186提供了一个RC.6 Plunker

关于angular - 新的 Angular2 路由器配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37270088/

相关文章:

javascript - 快照参数/服务在 Angular 2 中返回 NaN

angular - 在导航到另一条路线之前清除辅助路线。 @angular-4.0.2

Angular/ typescript : how to initialize Observable matrix

angular - 如何检查用户输入是否小于 Angular 2 中的最大值

angular - 如何更改 Angular2 中的特定内部路由参数

angular - 防止在不使用 RouteReuseStrategy 的情况下破坏路由上的组件状态

javascript - Angular 2 child 路由器 socket

angular - 如何在没有 TypeError : Cannot read property 'create' of undefined? 的情况下执行 ng-xi18n

Angular 4 Angular 火2

css - Angular 2 : change background color of div when checkbox is checked