我是角色新手,我正在练习延迟加载,我面临的问题是,当整个应用程序加载时,我会在最开始显示惰性组件,其中home组件应该是默认值。 在那之后,当我点击任何其他路线(家庭/约)并且他们得到正确显示但在那之后当我再次点击懒惰时它给我下面的错误
以下是我的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
答案 0 :(得分:1)
在您尝试导入RouterModule的懒惰模块中,它应该是RouterModule.forChild(路由)
答案 1 :(得分:1)
我知道这个问题已经回答了,但我有一个解决方案。
您还可以将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上创建了一个演示版。我希望这会有助于/指导你/他人。