1. 4
    Organize routes using the Routing Modules pattern in Angular
    5m 7s

Organize routes using the Routing Modules pattern in Angular

Share this video with your friends

Send Tweet

When our routing configuration gets more complex it is suggested to organize routes using the so-called “Routing Module” pattern. Simply speaking, this means moving the route definitions and everything else related to routing into its own dedicated NgModule, a sub module of the feature module you’re working on. In this lesson we will take a look how to migrate a simple routing configuration to reflect the Routing Module pattern.

Konstantinos
~ 6 years ago

Those 2 new modules both import the common module. however this common module is not imported into the app module. is it a good idea to import this common module into the app module and export it to the other modules?

Juri Strumpflohnerinstructor
~ 6 years ago

Well I think that's fine. Normally for the CommonModule this is how it is being done. But as you mention, there's nothing wrong for creating a dedicated module and exporting it from there and then in turn import that module everywhere else.

In fact, normally in an application you have what's often called SharedModule, where you import all of the modules that will be used throughout the application anyway. A typical example is the imports from the Angular Material library (if you're using that). Check out the official Angular docs about the NgModule FAQ for a detailed explanation on this.

BHRUGU DESAI
~ 6 years ago

You mentioned that, inside app.module.ts --> inside imports: [ ] - the order of the modules we import matters. I was not at all aware about it. Can you throw some light on it, how should we go about ordering them and why is the routing module added at the last. What should be the ideal order? I tried to find that on the documentation, but I didnt find much on it. thanks.

Juri Strumpflohnerinstructor
~ 6 years ago

Hey,

yes the ordering is important due to how the router resolves the routes. The reason is that the router tries to find a matching route using a depth-first algorithm. As such you might theoretically get different results, given you have multiple route configs that match the same URL.

But as said, normally this isn't an issue and in general I'd recommend to structure the routes in a way this doesn't happen (just to avoid confusion) :)