Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing between modules in Angular 2

Tags:

I'm writing an application where all features got it's own module (A feature could be a page, or a part of a page). This because we want all features to have it's own domain logic, services, directives and components, i.e. in the dashboard module we got an ChartComponent widget that I don't want to expose to other views like login or profile.

The problem is when working with routing in Angular 2 you always routes to a particular component, not a module.

In our case, to set up a route for path: '/dashboard' component: DashboardComponent we need to declare DashboardComponent in app.module.ts, and that's fine, but since we're still in the module app.module our CharComponent is not exposed and will not render in our DashboardComponent since it's declared in dashboard.module.ts and not app.module.ts.

If we declare ChartComponent in app.module.ts it's working as it should but we lost the architecture for our application.

The file structure for the application is something like this:

└─ src/    └─ app/       ├─ app.module.ts       ├─ app.component.ts       ├─ app.routing.ts       ├─ profile/       |  ├─ profile.module.ts       |  └─ profile.component.ts       ├─ login/       |  ├─ login.module.ts       |  └─ login.component.ts       └─ dashboard/          ├─ dashboard.module.ts          └─ dashboard.component.ts             └─ chart/                └─ chart.component.ts 
like image 438
bjorkblom Avatar asked Sep 01 '16 21:09

bjorkblom


People also ask

What is module routing in Angular?

In Angular, the best practice is to load and configure the router in a separate, top-level module. The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing. module.

What is RouterModule forRoot in Angular?

The forRoot() method creates an NgModule that contains all the directives, the given routes, and the Router service itself. The forChild() method creates an NgModule that contains all the directives and the given routes, but does not include the Router service.

What is the difference between forChild vs forRoot?

forRoot creates a module that contains all the directives, the given routes, and the router service itself. forChild creates a module that contains all the directives and the given routes, but does not include the router service.


2 Answers

It's not necessary to import components into main(app) module,

If you are loading routes lazily you may just define path like below,

// In app module route {  path: 'dashboard',  loadChildren: 'app/dashboard.module#DashboardModule' }  // in dashboard module const dashboardRoutes: Routes = [   { path: '',  component: DashboardComponent } ];  export const dashboardRouting = RouterModule.forChild(dashboardRoutes);  @NgModule({   imports: [    dashboardRouting   ],   declarations: [     DashboardComponent   ] }) export class DashboardModule { } 

OR

You may just import the DashboardModule in the main module and it will work if the routes are not lazily loaded.

@NgModule({   imports: [     BrowserModule,     FormsModule,     DashboardModule,     routing   ],   declarations: [     AppComponent   ],   providers: [     appRoutingProviders   ],   bootstrap: [ AppComponent ] }) export class AppModule { } 
like image 154
Madhu Ranjan Avatar answered Nov 09 '22 15:11

Madhu Ranjan


It turns out that lazy loading didn't work properly in RC5, just upgraded to RC6 and it all worked.

like image 28
bjorkblom Avatar answered Nov 09 '22 15:11

bjorkblom