Skyliver
Skyliver

Reputation: 5

Angular2 routing with lazy modules

I try to figure out how to configure routing in my application with lazy loaded modules.

For instance I have a lazy module called AnimalsModule, and I want that this module handle 2 routes "/cats" and "/dogs".

In current router api I need to specify common path for all lazy module routes, like: 'animals/dogs' and 'animals/cats'.

{
   path: "animals",
   loadChildren: "animals"
}

I can specify empty path for AnimalsModule:

{
    path: "",
    loadChildren: "animals"
},
{
   path: "foo",
   component: FooComponent
}

But in this case AnimalsModule will be unnecessary loaded when user navigates to "/foo" resource.

If I create 'animals/cats' and 'animals/dogs' routes and in the future I want to refactor my code to split AnimalsModule into DogsModule and CatsModule (also lazy), I will have to break my application routes. (same for merge)

How to create routing with lazy module without common path?

Upvotes: 0

Views: 286

Answers (1)

Meligy
Meligy

Reputation: 36594

Routes have a property called matcher property see docs, where you can provide your own route matching logic, but as in the comment of the question, I'd really either create 2 routes or even separate them into 2 modules (more likely).

Upvotes: 1

Related Questions