0
votes

I am new in Angular. I am trying to implement lazy loading with multiple component. Its lazy load home component properly but it fails to load another components.

It throws below error:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'admin'

app.routing.ts

{
   path: '',
   component: SiteLayoutComponent,
   children: 
   [
      {
        path: 'home',
        loadChildren: './lazymodules/lazy.module#LazyModule',
      },
   ]},

lazy-routing.module.ts

const routes: Routes = [
  {
    path: '', component: HomeComponent,
  }
   ,
   {
     path: 'admin', component: AdminComponent,
   },
  { path: 'settings', component: SettingsComponent },
  { path: 'about', component: AboutComponent }];     

I have created similar sample application on stackblitz. Anybody please hep here.

Thanks in advance

2

2 Answers

1
votes

Replace your navigation code (In site header file) with -

this.router.navigateByUrl('/home/admin');

or

this.router.navigate(['home/admin']); 
0
votes

Check the below changes I made in the site-header.component.ts

NavigateToRoute(pageURL: string) {
    //alert(pageURL);
    if (pageURL == "home") {
      console.log(pageURL);
      this.router.navigate(['home']),
      this.MenuActive = "home";
    }
    else if (pageURL == "admin") {
      console.log(pageURL);
      this.router.navigate(['admin'], {relativeTo: this.route}); // <- this was missing
      this.MenuActive = "admin";
    }
    else if (pageURL == "settings") {
      console.log(pageURL);
      this.router.navigate(['settings'], {relativeTo: this.route});
      this.MenuActive = "settings";
    }
    else if (pageURL == "about") {
      console.log(pageURL);
      this.router.navigate(['about'], {relativeTo: this.route});
      this.MenuActive = "about";
    }
    else if (pageURL == "login") {
      console.log(pageURL);
      this.router.navigate(['login'], {relativeTo: this.route});
    }
  }

Have a look at the updated example here