新的Angular2路由器配置 [英] New Angular2 router configuration

查看:56
本文介绍了新的Angular2路由器配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用不赞成使用的路由器时,我可以执行router.config并传入一个对象.问题是,在启动应用程序后,路由器本身已进行了配置,该对象具有与我使用@RouterConfig相同的模板".我正在寻找的是是否有办法像这样配置新路由器.一直在浏览文档,但由于尚未进行文档记录,我有点茫然.

Back when using the deprecated router I was able to do a router.config and pass in an object. Thing is, the router itself got configured a bit after the app was started, the object had the same "template" as if I'd used the @RouterConfig. What I'm looking for is if there is a way to config the new router like this. Been looking through the documentation but I'm a bit at a loss since it isn't documented yet.

根据答案进行编辑

Edit due to answer

不,我不能使用@Routes.问题是我在构造路由器之后加载配置.这是我如何使用旧路由器的摘要:

No, I can't use @Routes. Thing is I'm loading the configuration after the construction of the router. Here is a snipped of how I did it with the old router:

       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),然后将其发送到路由器.我正在寻找一种对新路由器执行相同操作的方法.

As you can see, I'm building myself a json object ( RouterComponents ) and then sending it to the router. I'm looking for a way to do the same with the new router, or something alike.

推荐答案

在新路由器(>= RC.3)中

In the new router (>= RC.3) https://angular.io/docs/ts/latest/api/router/index/Router-class.html resetConfig can be used

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

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

You might need to provide some dummy router configuration to not get errors on application startup.

https://github.com/angular/angular/issues/11437 #issuecomment-245995186 提供了 RC.6 柱塞

这篇关于新的Angular2路由器配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆