I am learning Angular 2's app-routing from official documentation. I am going through following piece of code.
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; export const routes: Routes = [ { path: '', redirectTo: 'contact', pathMatch: 'full'}, { path: 'crisis', loadChildren: 'app/crisis/crisis.module#CrisisModule' }, { path: 'heroes', loadChildren: 'app/hero/hero.module#HeroModule' } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
It creates an AppRoutingModule
and defines routes
for it. The part which I am not able to understand is Why do we need to export RouterModule again? I believe it's one of the core angular module and which available everywhere by import of @angular/router
.
RouterModulelink. Adds directives and providers for in-app navigation among views defined in an application. Use the Angular Router service to declaratively specify application states and manage state transitions.
RouterModule (enableTracing) You can set enableTracing to RouterModule which will log you all the route change events.
Angular Routinglink As users perform application tasks, they need to move between the different views that you have defined. To handle the navigation from one view to the next, you use the Angular Router . The Router enables navigation by interpreting a browser URL as an instruction to change the view.
import { RouterModule} from '@angular/router'RouterModule refers to the forRoot which takes an input as an array, which in turn has the object of the path and the component. Path is the name of the router and component is the name of the class, i.e., the component created.
You don't need to export it. It's just for convenience. If you add AppRoutingModule
to AppModule
you also implicitly import RouterModule
this way. Otherwise you would need to import it explicitely
@NgModule({ imports: [AppRoutingModule, RouterModule], }) export class AppModule {}
for example to be able to use <router-outlet>
or RouterLink
in components declared in AppModule
Do you need a Routing Module?
The Routing Module replaces the routing configuration in the root or feature module. Either configure routes in the Routing Module or within the module itself but not in both.
The Routing Module is a design choice whose value is most obvious when the configuration is complex and includes specialized guard and resolver services. It can seem like overkill when the actual configuration is dead simple.
Some developers skip the Routing Module (for example, AppRoutingModule) when the configuration is simple and merge the routing configuration directly into the companion module (for example, AppModule).
Choose one pattern or the other and follow that pattern consistently.
Most developers should always implement a Routing Module for the sake of consistency. It keeps the code clean when configuration becomes complex. It makes testing the feature module easier. Its existence calls attention to the fact that a module is routed. It is where developers expect to find and expand routing configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With