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

问题描述

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

因回答而编辑

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

 myLoader.loadComponentConfig(configPath).then(components => { self.Components = components;components.map(comp => {self.RouterComponents.push({路径:'/' + comp.name,组件:comp,如: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([{ 路径:'团队/:id',组件:TeamCmp,孩子:[{ 路径:'简单',组件:SimpleCmp },{ 路径:'用户/:名称',组件:UserCmp }] }]);

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

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

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

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

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.

解决方案

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

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 provides an RC.6 Plunker

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

07-17 14:11
查看更多