延迟加载不适用于 angular 4
Lazy loading is not working for angular 4
我是 angular 的新手,我正在练习延迟加载,我面临的问题是我在整个应用程序加载的一开始就显示了延迟组件,而 home 组件应该是默认值。
在那之后,当我点击任何其他路线(home/about)时,它们会正确显示,但在那之后,当我再次点击懒惰时,它会给我以下错误
下面是我的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 { }
我的延迟加载模块:
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 { }
我没有添加我正在导航的位置,因为没有错误,否则没有任何效果。
请告诉我我做错了什么??
添加完整的完整错误消息:
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)
我知道这个问题已经得到解答,但我有一个解决方案。
您还可以使用 loadChildren
来加载像
这样的惰性模块
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)
在其他模块中
const routes : Routes=[
{path:"",redirectTo : 'lazy',pathMatch:"full"},
{path :"lazy",component:LazyComponent}
];
RouterModule.forChild(routes)
我在 stackblitz 上创建了一个演示。我希望这会 help/guide 到 you/others。
我是 angular 的新手,我正在练习延迟加载,我面临的问题是我在整个应用程序加载的一开始就显示了延迟组件,而 home 组件应该是默认值。 在那之后,当我点击任何其他路线(home/about)时,它们会正确显示,但在那之后,当我再次点击懒惰时,它会给我以下错误
下面是我的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 { }
我的延迟加载模块:
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 { }
我没有添加我正在导航的位置,因为没有错误,否则没有任何效果。
请告诉我我做错了什么??
添加完整的完整错误消息:
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)
我知道这个问题已经得到解答,但我有一个解决方案。
您还可以使用 loadChildren
来加载像
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)
在其他模块中
const routes : Routes=[
{path:"",redirectTo : 'lazy',pathMatch:"full"},
{path :"lazy",component:LazyComponent}
];
RouterModule.forChild(routes)
我在 stackblitz 上创建了一个演示。我希望这会 help/guide 到 you/others。