Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: Export of RouterModule, Why it is required?

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.

like image 527
Hitesh Kumar Avatar asked Jan 24 '17 08:01

Hitesh Kumar


People also ask

What is the use of RouterModule in Angular?

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.

What is the use of enableTracing in RouterModule?

RouterModule (enableTracing) You can set enableTracing to RouterModule which will log you all the route change events.

Why do we need Angular routing?

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.

What happens when router module is imported?

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.


2 Answers

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

like image 113
Günter Zöchbauer Avatar answered Oct 11 '22 05:10

Günter Zöchbauer


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.

like image 24
LucindaZhang Avatar answered Oct 11 '22 03:10

LucindaZhang