2

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

Pratik Kelwalkar
  • 1,592
  • 2
  • 15
  • 21
  • no need to use `useDefault: false` . – Pardeep Jain Apr 01 '16 at 05:37
  • 1
    You could try to add some arbitrary default route statically with a name that doesn't conflict with any of the loaded routes, then after or before loading the routes from the service and then deactivate the statically added route. I'm not really sure the `deactivate()` does the right thing here though https://angular.io/docs/ts/latest/api/router/Router-class.html – Günter Zöchbauer Apr 01 '16 at 05:38
  • So you mean to say that i hardcode a static route(default), which will have a empty page, then later deactive it and replace it with another (default) ? – Pratik Kelwalkar Apr 01 '16 at 06:02
  • @Günter Zöchbauer could you provide me a snippet as i am unable use router.deactivate – Pratik Kelwalkar Apr 01 '16 at 11:15
  • I don't know if it is the right tools for your requirement. I haven't used it myself. I just got it suggested for a similar use case but I didn't follow that approach for other reasons. What happens? Do you get an error message? What have you tried? – Günter Zöchbauer Apr 01 '16 at 11:16

0 Answers0