2

I have a Simple Angular 2 App with a Module(InformationPagesModule module) that contains two lazy load components (Info1 Component and Info2 Component). I want to load that components when entering the following routes in the browser:

http://localhost:4200/info1 - Loads the Info1Component

http://localhost:4200/info2 - Loads the Info2Component

here the interest classes:

app.component.html

<router-outlet></router-outlet>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { InformationPagesModule } from './information-pages/information-pages.module';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    SharedModule,
    InformationPagesModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  {path:'', component: HomeComponent},
  {path:'info1', loadChildren: './information-pages/information-pages.module#InformationPagesModule'},
  {pathMatch:'full', path:'info2', loadChildren: './information-pages/information-pages.module#InformationPagesModule'},
  { path: '**', redirectTo: '' }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],

})
export class AppRoutingModule { }

information-pages.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { InformationPagesRoutingModule } from './information-pages-routing.module';
import { Info1Component } from './info1/info1.component';
import { Info2Component } from './info2/info2.component';

@NgModule({
  imports: [
    CommonModule,
    InformationPagesRoutingModule
  ],
  declarations: [Info1Component, Info2Component]
})
export class InformationPagesModule { }

information-pages-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Info1Component } from './info1/info1.component';
import { Info2Component } from './info2/info2.component';

const routes: Routes = [
  {path:'info1', component: Info1Component},
  {path:'info2', component: Info2Component}
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class InformationPagesRoutingModule { }

My question is, why when entering some of the routes / info1 or / info2, a blank page is displayed?

How should I configure the routes in my application so that it works?

Many Thanks!

4
  • Can u create stackblitz example please ? Commented Apr 27, 2020 at 1:32
  • Can you try to remove the last { path: '**', redirectTo: '' } Also, if you are using the newest version of Angular, you should change the way you loadChildren: angular.io/guide/… Commented Apr 27, 2020 at 2:02
  • I have problems with the routes in stackblitz Commented Apr 27, 2020 at 2:17
  • Im try to remove the last { path: '**', redirectTo: '' }, but not working the same problem. Im using angular 2 Commented Apr 27, 2020 at 2:30

2 Answers 2

2

In your app-routing.module.ts you want to declare one parent route to all routes of one lazy loaded module. That means you might want to map all routes starting with /info to your information-pages-routing.module.ts. The routes to your components (info1 and info2) you'll declare inside the lazy loaded module.

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  { path:'', component: HomeComponent },
  { path: 'info', loadChildren: './information-pages/information-pages.module#InformationPagesModule' },
  { path: '**', redirectTo: '' }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],

})
export class AppRoutingModule { }

The file information-pages-routing.module.ts stays the same

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Info1Component } from './info1/info1.component';
import { Info2Component } from './info2/info2.component';

const routes: Routes = [
  {path:'info1', component: Info1Component},
  {path:'info2', component: Info2Component}
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class InformationPagesRoutingModule { }

You'll now be able to access the Info1Component by the route /info/info1 and the Info2Component by the route /info/info2. If you wish to access your compoents by the routes /info1 and /info2 you can either create two lazy loaded modules or just redirect /info1 to /info/info1


You might also consider loading child modules like this:

{ path: 'info', loadChildren: () => import('./information-pages/information-pages.module').then(m => m.InformationPageModule) }

Rather than

{ path: 'info', loadChildren: './information-pages/information-pages.module#InformationPagesModule' }

For more information please refer to the official documentation https://angular.io/guide/lazy-loading-ngmodules

Sign up to request clarification or add additional context in comments.

Comments

-1

Use this way instead of using module level routing, use AppRoutingModule for routing your components.

import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { Info1Component } from './info1/info1.component';
import { Info2Component } from './info2/info2.component';

const routes: Routes = [
  {path:'', component: HomeComponent},
  {path:'info1', component: Info1Component},
  {path:'info2', component: Info2Component},
  { path: '**', redirectTo: '' }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],

})
export class AppRoutingModule { }

If you use this information-pages-routing.module.ts is not required any more.

1 Comment

I need to use a lazy loading for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.