我正在尝试将懒惰路由应用到我的应用程序中 .
我有一个非常大的项目,当它在路由器已弃用时,我使用了AsyncRoute,但现在它已被删除 .
所以我试着实现最新的延迟加载,但是我遇到了一个问题 RangeError: Maximum call stack size exceeded 我做错了什么?我在指令中做了所有代码 .
请看一下
EncountersModule
import { NgModule } from '@angular/core';
// import { CommonModule } from '@angular/common';
/* --------------- !System modules --------------- */
import { SharedModule } from 'sharedModule'; //There is a lot of shared components/directives/pipes (over 60) and it re-exports CommonModule so I can't avoid it
/* --------------- !App outer modules --------------- */
import { EncountersComponent } from './encounters.component';
// import { PassCodeComponent } from '../../shared/components/passcode/passcode.component';
@NgModule({
imports: [ SharedModule ],
declarations: [ EncountersComponent],
exports: [ EncountersComponent ],
})
export class EncountersModule { }
这是我的 app.routing.module
import { NgModule } from '@angular/core';
// import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ImagingComponent } from '../modules/index';
import { DashboardComponent } from '../modules/index';
import { PrescriptionNoticesComponent } from '../modules/index';
// import { EncountersComponent } from "../modules/encounters/encounters.component";
import { ScheduleComponent } from "../modules/schedule/schedule.component";
import { AdminComponent } from '../modules/index';
@NgModule({
imports: [
RouterModule.forRoot([
{
path: '',
component: DashboardComponent,
data: { label: 'Dashboard' }
},
{
path: 'encounters',
// component: EncountersComponent,
loadChildren: 'production/modules/encounters/encounters.module#EncountersModule',
data: { label: 'Encounters' }
},
{
path: 'admin',
component: AdminComponent,
data: { label: 'Admin' }
}
])
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}
// const appRoutes: Routes = [
// {
// path: 'imaging',
// component: ImagingComponent,
// data: { label: 'Imaging' }
// },
// {
// path: '',
// component: DashboardComponent,
// data: { label: 'Dashboard' }
// },
// {
// path: 'prescription_notices',
// component: PrescriptionNoticesComponent,
// data: { label: 'Prescription Notices' }
// },
// {
// path: 'encounters',
// component: EncountersComponent,
// data: { label: 'Encounters' }
// },
// {
// path: 'schedule',
// component: ScheduleComponent,
// data: { label: 'Schedule' }
// },
// {
// path: 'admin',
// component: AdminComponent,
// data: { label: 'Admin' }
// }
// ];
//
// export const appRoutingProviders: any[] = [
//
// ];
//
// export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
3 回答
loadChildren需要引用带路由的模块
通过为路由中的loadChildren属性赋值,您必须引用一个已实现路由系统的模块 . 换句话说,只引用一个导入RoutingModule并使用forChild(routes)方法配置它的模块 .
我不确定,因为它没有在文档(2.4.2)中明确提到,但是从“Angular Modules”和“Routing&Navigation”指南中的示例中,我引出了以下模式:
懒惰模块应该有自己的路由模块 .
"lazy-routing.module"中定义的routes数组应该只有一个元素;该元素的
path
属性应为空字符串;应该定义component
属性(当惰性模块提供任何服务以使注入工作正常时是必需的)并且引用组件的模板应该具有带有<router-outlet>
指令的元素 . 此路由通常具有children
属性 ."app-routing.module"(在我的示例中为"lazyModulePrefix")中定义的惰性路由的
path
属性的值将是".lazy-routing.module"中定义的所有路径的前缀 .例如:
.
.
.
使用上面的代码,路由映射将是:
http://host/login - > LoginComponent
http://host/lazyModulePrefix - > LazyModuleHomeComponent
http://host/lazyModulePrefix/somePath1 - > AComponentDeclaredInTheLazyModule1
http://host/lazyModulePrefix/somePath2 - > AComponentDeclaredInTheLazyModule2
http://host/anythingElse - > PageNotFoundComponent
尝试删除评论 . 当我在我正在处理的应用程序中将路由器更新为当前时,我在旧路由器上评论了一堆东西,因为我不想丢失它 . 删除评论后,一些奇怪的错误消失了 .