Angular2 App Routing through Services

2019-05-29 02:07发布

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..

0条回答
登录 后发表回答