简体   繁体   中英

Lazy loading is not working for angular 4

I am new to angular , I was practicing lazy loading , the issue i AM facing is I get the lazy component displayed at the very beginning when the whole app loads ,where home component is supposed to be the default . After that when I am clicking any other route (home/about) and they get displayed properly but after that when I click on lazy again it gives me the below error

在此处输入图片说明

Below is my app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import{aboutComp} from './routingNav/aboutComp'
import {homeComp} from './routingNav/homeComp'
import { AppComponent } from './app.component';
import{Routes,RouterModule} from '@angular/router';
import { CustomersRoutingModule } from './lazyComp/lazyModule';

const routes : Routes=[
{path:"",redirectTo : '/home',pathMatch:"full"},
{path :"home",component:homeComp},
{path:"about",component:aboutComp},
{path:"lazy",loadChildren:'./lazyComp/lazyModule#CustomersRoutingModule'}
];
@NgModule({
 declarations: [
 AppComponent,
 aboutComp,
 homeComp,    ],
 imports: [
 BrowserModule,
 RouterModule.forRoot(routes),
 CustomersRoutingModule
 ],
 providers: [],
 bootstrap: [AppComponent]
 })
export class AppModule { }

My lazy load module :

 import { NgModule } from '@angular/core';
 import {Routes,RouterModule} from '@angular/router'; 
 import{lazyComp} from './lazyComp'
 const routes1 : Routes=[
 {path:"",component:lazyComp}
 ]

 @NgModule({
 declarations: [
lazyComp
],
 imports:[RouterModule.forRoot(routes1)],
 exports:[RouterModule]
 })
 export class CustomersRoutingModule { }

I am not adding where I am doing navigation as there is no error otherwise nothing would work.

Please let me know what am I doing wrong ??

Adding the full full error msg :

 core.js:1449 ERROR Error: Uncaught (in promise): TypeError: undefined is 
 not a function
TypeError: undefined is not a function
at Array.map (<anonymous>)
at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive 
(main.bundle.js:13), <anonymous>:10:34)
at SystemJsNgModuleLoader.loadAndCompile (core.js:6570)
at SystemJsNgModuleLoader.load (core.js:6554)
at RouterConfigLoader.loadModuleFactory (router.js:4595)
at RouterConfigLoader.load (router.js:4575)
at MergeMapSubscriber.eval [as project] (router.js:2061)
at MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.Subscriber.next (Subscriber.js:95)
at Array.map (<anonymous>)
at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive 
(main.bundle.js:13), <anonymous>:10:34)
at SystemJsNgModuleLoader.loadAndCompile (core.js:6570)
at SystemJsNgModuleLoader.load (core.js:6554)
at RouterConfigLoader.loadModuleFactory (router.js:4595)
at RouterConfigLoader.load (router.js:4575)
at MergeMapSubscriber.eval [as project] (router.js:2061)
at MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.Subscriber.next (Subscriber.js:95)
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at eval (zone.js:873)
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4751)
at ZoneDelegate.invokeTask (zone.js:420)
at Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)
at ZoneTask.invokeTask [as invoke] (zone.js:500)
at invokeTask (zone.js:1540)
 defaultErrorLogger @ core.js:1449
 ErrorHandler.handleError @ core.js:1510
 next @ core.js:5508
 schedulerFn @ core.js:4342
 SafeSubscriber.__tryOrUnsub @ Subscriber.js:243
 SafeSubscriber.next @ Subscriber.js:190
Subscriber._next @ Subscriber.js:131
Subscriber.next @ Subscriber.js:95
Subject.next @ Subject.js:56
EventEmitter.emit @ core.js:4322
(anonymous) @ core.js:4782
ZoneDelegate.invoke @ zone.js:388
Zone.run @ zone.js:138
NgZone.runOutsideAngular @ core.js:4708
onHandleError @ core.js:4782
ZoneDelegate.handleError @ zone.js:392
Zone.runGuarded @ zone.js:154
  _loop_1 @ zone.js:677
  api.microtaskDrainDone @ zone.js:686
 drainMicroTaskQueue @ zone.js:602
 ZoneTask.invokeTask @ zone.js:500
 invokeTask @ zone.js:1540
 globalZoneAwareCallback @ zone.js:1566

在您尝试导入RouterModule的惰性模块中,它应该是RouterModule.forChild(routes)

I know this question is already answered but I have one solution for this same.

You can also use loadChildren for load lazy modules like

import {CustomersModule} from './customers/customers.module'
const routes : Routes=[
   {path:"",redirectTo : 'home',pathMatch:"full"},
   {path :"home",component:HomeComponent},
   {path:"about",component:AboutComponent},
   {path:"lazy", loadChildren: () => CustomersModule}
];
RouterModule.forRoot(routes)

in other modules

   const routes : Routes=[
      {path:"",redirectTo : 'lazy',pathMatch:"full"},
      {path :"lazy",component:LazyComponent}
   ];
   RouterModule.forChild(routes)

I have created a demo on stackblitz . I hope this will help/guide to you/others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM