Angular 延迟加载模块 - child 路由中的问题
Angular Lazy loaded module - Issue in a child route
我有一些模块:"dashboard"、"family" 和 "children"。这些模块在我的 appRoute 中延迟加载,除了 "childen" 模块:
export const appRoutes: Routes = [
{ path: '', loadChildren: 'app/dashboard/dashboard.module#DashboardModule' },
{ path: 'family', loadChildren: 'app/family/family.module#FamilyModule' },
{
path: '404',
component: Error404PageComponent,
resolve: { data: Error404PageResolver }
},
{
path: '**',
component: Error404PageComponent,
resolve: { data: Error404PageResolver }
}
];
@NgModule({
imports: [ RouterModule.forRoot(appRoutes, {
preloadingStrategy: PreloadAllModules,
useHash: false
}) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
我家的路线文件是这样的:
const familyRoutes: Routes = [
{
path: '',
component: FamilyComponent
{
path: ':id',
component: FamilleComponent
];
@NgModule({
imports: [RouterModule.forChild(familyRoutes)],
exports: [RouterModule]
})
export class FamilyRoutingModule { }
对于我的 "children" 模块,它几乎是一样的,除了路由,路由的第一部分包含家庭路由:
const childRoutes: Routes = [
{
path: 'family/:familyId/child',
component: ChildComponent
},
{
path: 'family/:familyId/child/:id',
component: ChildComponent
}
];
我想延迟加载 "children" 模块,但我不知道该怎么做,因为该模块是模块 "family" 的 child。
我的问题是链接到 child 的路线不可用。
所以 url
http://localhost:4200/family/830503261/child/830581020 无效。
我尝试在 familyRoutes 中添加 loadChildren 行,但不起作用:
const familleRoutes: Routes = [
{
path: '',
component: FamilyComponent
{
path: ':id',
component: FamilyComponent,
loadChildren: './../child/child.module#ChildModule'
];
我不知道如何使用路由中的路径 "family" 访问我的 children。
有什么想法吗?
谢谢。
您应该将您的 childModule 配置为 children 的 family route
const familyRoutes: Routes = [
{
path: '',
component: FamilyComponent
},
{
path: ':id',
component: FamilleComponent,
children: [
'path': 'child',
'loadChildren': './../child/child.module#ChildModule', // <- or whatever path
]
}
];
@NgModule({
imports: [RouterModule.forChild(familyRoutes)],
exports: [RouterModule]
})
export class FamilyRoutingModule { }
并更改您的子模块的路由
const childRoutes: Routes = [
{
path: '',
component: ChildComponent
},
{
path: ':id',
component: ChildComponent
}
];
感谢您的回复。这是工作。我只是添加 {}:
children: [
{
path: 'child',
loadChildren: './../child/child.module#ChildModule'
}
],
我还有一个问题。在我的子路由文件中,我还有一个守卫和其他东西:
const childRoutes: Routes = [
{
path: '',
component: ChildComponent,
resolve: { user: AuthResolver, referentiel: ReferentielChildResolver },
canActivate: [ChildActivateGuard]
},
{
path: ':id',
component: ChildComponent,
resolve: { user: AuthResolver, child: ChildResolver, referentiel: ReferentielChildResolver },
canActivate: [ChildActivateGuard]
}
];
在我的 ChildActivateGuard 中,我想拥有我家人的 ID:
Injectable()
export class EleveActivateGuard implements CanActivate {
can: boolean;
constructor(private childService: ChildService, public http: HttpService)
{ }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
debugger;
console.log(route.parent.params["familyId"]);
....
}
但是参数 familyId 未定义。我不明白缺少什么?
谢谢您的帮助。
谢谢皮埃尔的评论。
我找到了解决办法。即使我不确定这是最好的解决方案,但它在我的子组件的保护文件中工作:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
var familyIdId = route.parent.parent.url[0].path;
...
我有一些模块:"dashboard"、"family" 和 "children"。这些模块在我的 appRoute 中延迟加载,除了 "childen" 模块:
export const appRoutes: Routes = [
{ path: '', loadChildren: 'app/dashboard/dashboard.module#DashboardModule' },
{ path: 'family', loadChildren: 'app/family/family.module#FamilyModule' },
{
path: '404',
component: Error404PageComponent,
resolve: { data: Error404PageResolver }
},
{
path: '**',
component: Error404PageComponent,
resolve: { data: Error404PageResolver }
}
];
@NgModule({
imports: [ RouterModule.forRoot(appRoutes, {
preloadingStrategy: PreloadAllModules,
useHash: false
}) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
我家的路线文件是这样的:
const familyRoutes: Routes = [
{
path: '',
component: FamilyComponent
{
path: ':id',
component: FamilleComponent
];
@NgModule({
imports: [RouterModule.forChild(familyRoutes)],
exports: [RouterModule]
})
export class FamilyRoutingModule { }
对于我的 "children" 模块,它几乎是一样的,除了路由,路由的第一部分包含家庭路由:
const childRoutes: Routes = [
{
path: 'family/:familyId/child',
component: ChildComponent
},
{
path: 'family/:familyId/child/:id',
component: ChildComponent
}
];
我想延迟加载 "children" 模块,但我不知道该怎么做,因为该模块是模块 "family" 的 child。 我的问题是链接到 child 的路线不可用。 所以 url http://localhost:4200/family/830503261/child/830581020 无效。
我尝试在 familyRoutes 中添加 loadChildren 行,但不起作用:
const familleRoutes: Routes = [
{
path: '',
component: FamilyComponent
{
path: ':id',
component: FamilyComponent,
loadChildren: './../child/child.module#ChildModule'
];
我不知道如何使用路由中的路径 "family" 访问我的 children。
有什么想法吗? 谢谢。
您应该将您的 childModule 配置为 children 的 family route
const familyRoutes: Routes = [
{
path: '',
component: FamilyComponent
},
{
path: ':id',
component: FamilleComponent,
children: [
'path': 'child',
'loadChildren': './../child/child.module#ChildModule', // <- or whatever path
]
}
];
@NgModule({
imports: [RouterModule.forChild(familyRoutes)],
exports: [RouterModule]
})
export class FamilyRoutingModule { }
并更改您的子模块的路由
const childRoutes: Routes = [
{
path: '',
component: ChildComponent
},
{
path: ':id',
component: ChildComponent
}
];
感谢您的回复。这是工作。我只是添加 {}:
children: [
{
path: 'child',
loadChildren: './../child/child.module#ChildModule'
}
],
我还有一个问题。在我的子路由文件中,我还有一个守卫和其他东西:
const childRoutes: Routes = [
{
path: '',
component: ChildComponent,
resolve: { user: AuthResolver, referentiel: ReferentielChildResolver },
canActivate: [ChildActivateGuard]
},
{
path: ':id',
component: ChildComponent,
resolve: { user: AuthResolver, child: ChildResolver, referentiel: ReferentielChildResolver },
canActivate: [ChildActivateGuard]
}
];
在我的 ChildActivateGuard 中,我想拥有我家人的 ID:
Injectable()
export class EleveActivateGuard implements CanActivate {
can: boolean;
constructor(private childService: ChildService, public http: HttpService)
{ }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
debugger;
console.log(route.parent.params["familyId"]);
....
}
但是参数 familyId 未定义。我不明白缺少什么? 谢谢您的帮助。
谢谢皮埃尔的评论。
我找到了解决办法。即使我不确定这是最好的解决方案,但它在我的子组件的保护文件中工作:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
var familyIdId = route.parent.parent.url[0].path;
...