回到使用已弃用的路由器时,我能够执行 router.config 并传入一个对象。事实是,路由器本身在应用程序启动后进行了一些配置,该对象具有相同的“模板”,就像我使用了 @RouterConfig 一样。我正在寻找的是是否有一种方法可以像这样配置新路由器。一直在查看文档,但我有点不知所措,因为它还没有记录。
由于答案而编辑
不,我不能使用@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 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 https://github.com/angular/angular/issues/11437#issuecomment-245995186提供了一个RC.6 Plunker
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)