在使用已弃用的路由器时,我能够执行 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/