Angular 4 延迟加载子组件未加载
Angular 4 Lazy Loading Child Components Not Loaded
我有一个中等大小的 Angular 应用程序。这在加载时花费了很多时间。因此我决定使用延迟加载。我有一个延迟加载的 FeedbackModule
。它看起来像这样:
反馈途径:
export const FEEDBACK_ROUTES: Routes = [
{ path : '' , component : FeedbackComponent},
{ path : 'prebilling' , component : PrebillingComponent},
{ path : 'postbilling/login' , component : PostbillingComponentLogin},
{ path : 'postbilling/rating/:mid/:return/:mtid' , component : PostbillingRatingComponent},
{ path : 'prebilling/rating/:mid/:type/:mtid/:mcnt/:mebid' , component : PrebillingRatingComponent},
{ path : 'prebilling/rating' , component : PrebillingRatingComponent},
{ path : 'postbilling/rating/:id' , component : PostbillingRatingComponent},
{ path : 'prebilling/rating/:mid/:type/:mtid' , component : PrebillingRatingComponent},
{ path : 'thanks/:id' , component : ThankYouComponent}
];
反馈模块:
@NgModule({
declarations: [
PostbillingComponentLogin,
PrebillingComponent,
PrebillingRatingComponent,
PostbillingRatingComponent,
ThankYouComponent,
FeedbackComponent,
PostbillingForgotPassComponentLogin
],
imports: [
CommonModule,
CommonCustomModule,
FormsModule,
RouterModule.forChild(FEEDBACK_ROUTES)
],
exports:[ RouterModule]
})
export class FeedbackModule {
}
App.route.ts:
export const ROUTES : Routes = [
...COMMON_ROUTES,
{ path:'feedback', loadChildren: './feedBack/feedback.module#FeedbackModule'}
]
现在,当我获取路径 /feedback
时,反馈组件已加载。但是当我为 /feedback/prebilling
或任何其他路径时,它仍然加载 FeedbackComponent。提前致谢!
这是因为您没有将其他路由作为延迟加载模块的 child 路由
常量路线:路线= [
{
路径:'',组件:OrgComponent,
child仁:[
{ path: 'intro', loadChildren: '../+intro/intro.module#IntroModule' },
{ 路径:'recent',组件:RecentComponent },
]
}
]
我觉得你的代码没问题,但你可以尝试添加 pathMatch: full
{ path : '' , component : FeedbackComponent, pathMatch: 'full' }
让我知道它是否适合你...
我有一个中等大小的 Angular 应用程序。这在加载时花费了很多时间。因此我决定使用延迟加载。我有一个延迟加载的 FeedbackModule
。它看起来像这样:
反馈途径:
export const FEEDBACK_ROUTES: Routes = [
{ path : '' , component : FeedbackComponent},
{ path : 'prebilling' , component : PrebillingComponent},
{ path : 'postbilling/login' , component : PostbillingComponentLogin},
{ path : 'postbilling/rating/:mid/:return/:mtid' , component : PostbillingRatingComponent},
{ path : 'prebilling/rating/:mid/:type/:mtid/:mcnt/:mebid' , component : PrebillingRatingComponent},
{ path : 'prebilling/rating' , component : PrebillingRatingComponent},
{ path : 'postbilling/rating/:id' , component : PostbillingRatingComponent},
{ path : 'prebilling/rating/:mid/:type/:mtid' , component : PrebillingRatingComponent},
{ path : 'thanks/:id' , component : ThankYouComponent}
];
反馈模块:
@NgModule({
declarations: [
PostbillingComponentLogin,
PrebillingComponent,
PrebillingRatingComponent,
PostbillingRatingComponent,
ThankYouComponent,
FeedbackComponent,
PostbillingForgotPassComponentLogin
],
imports: [
CommonModule,
CommonCustomModule,
FormsModule,
RouterModule.forChild(FEEDBACK_ROUTES)
],
exports:[ RouterModule]
})
export class FeedbackModule {
}
App.route.ts:
export const ROUTES : Routes = [
...COMMON_ROUTES,
{ path:'feedback', loadChildren: './feedBack/feedback.module#FeedbackModule'}
]
现在,当我获取路径 /feedback
时,反馈组件已加载。但是当我为 /feedback/prebilling
或任何其他路径时,它仍然加载 FeedbackComponent。提前致谢!
这是因为您没有将其他路由作为延迟加载模块的 child 路由
常量路线:路线= [ { 路径:'',组件:OrgComponent, child仁:[ { path: 'intro', loadChildren: '../+intro/intro.module#IntroModule' }, { 路径:'recent',组件:RecentComponent }, ] } ]
我觉得你的代码没问题,但你可以尝试添加 pathMatch: full
{ path : '' , component : FeedbackComponent, pathMatch: 'full' }
让我知道它是否适合你...