Angular2 App Routing through Services

…衆ロ難τιáo~ 提交于 2019-12-07 19:16:06

问题


Here's my scenario :

Il get straight to the point first... I want to load all the routes including the default route of a app in RouteConfig through a service. I am able to do so but cannot load the default route dynamically, other routes can be loaded dynamically.

Suppose i bootstrap(AppComponent) and my AppComponent view only content is <router-outlet></router-outlet> so in this scenario the AppComponent RouteConfig needs to be loaded dynamically i.e in my case i call a service which returns a JSON in this format

[
  { "path" : "/child1" , "name" : "Child1" , "component" : "ChildComponent1", "resource" : "./app/dash/child1/child1.component",  "default" : true },
  { "path" : "/child2" , "name" : "Child2" , "component" : "ChildComponent2", "resource" : "./app/dash/child2/child2.component", "default" : false }
]

In the above scenario i am able to make it working only by hardcoding the AppComponent RouteConfig like this :

@RouteConfig([
     {path : '/child1' , name : 'Child1' , component : ChildComponent1 , useAsDefault : true}
])

export class AppComponent{}

and then from load the remaining paths from service, which works but doesn't suffice my scenario. I want to load the useAsDefault too to the router but then it fails. So basically i want to load and configure all my app routes dynamically.Any suggestion.

P.S I do have a follow up question for the same in a parent-child scenario..

来源:https://stackoverflow.com/questions/36348793/angular2-app-routing-through-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!