@angular/router 的 loadChildren 中的 gen 路径名
gen pathname within loadChildren of @angular/router
我在 loadChildren 中使用延迟加载,我可以让不同的页面具有相同的相似 url。下面是我如何分离它的示例:
{
path: ':any',
loadChildren: () => {
if(window.location.pathname.toLowerCase().indexOf('/some')==0){
return import('some/path.module').then(m => m.Module)
}else{
return import('some/other/path.module').then(m => m.Module)
}
}
}
我面临的问题是当我直接加载此页面时,一切都按预期工作,但如果我在其他页面上并使用本地重定向,在 window.location.pathname
中我们有之前的 url.
是否有 Angular 方法在 loadChildren
中拉取新的 url?
也许,有可能以某种超时方式保持 loadChildren
?
我认为解决它的一种方法是使用 canLoad
守卫。
// Inside your `canLoad` guard
constructor (private router: Router) { }
canLoad(route: Route, segments: UrlSegment[]) {
if (cond1...) {
this.router.navigate([urlForModule1])
} else {
this.router.navigate([urlForModule2])
}
}
您仍然需要在父路由模块中定义路由。
{ path: 'some/module', canLoad: [canLoadModule], loadChildren: () => ... },
{ path: 'some/other/module', canLoad: [canLoadModule], loadChildren: () => ... }
我在 loadChildren 中使用延迟加载,我可以让不同的页面具有相同的相似 url。下面是我如何分离它的示例:
{
path: ':any',
loadChildren: () => {
if(window.location.pathname.toLowerCase().indexOf('/some')==0){
return import('some/path.module').then(m => m.Module)
}else{
return import('some/other/path.module').then(m => m.Module)
}
}
}
我面临的问题是当我直接加载此页面时,一切都按预期工作,但如果我在其他页面上并使用本地重定向,在 window.location.pathname
中我们有之前的 url.
是否有 Angular 方法在 loadChildren
中拉取新的 url?
也许,有可能以某种超时方式保持 loadChildren
?
我认为解决它的一种方法是使用 canLoad
守卫。
// Inside your `canLoad` guard
constructor (private router: Router) { }
canLoad(route: Route, segments: UrlSegment[]) {
if (cond1...) {
this.router.navigate([urlForModule1])
} else {
this.router.navigate([urlForModule2])
}
}
您仍然需要在父路由模块中定义路由。
{ path: 'some/module', canLoad: [canLoadModule], loadChildren: () => ... },
{ path: 'some/other/module', canLoad: [canLoadModule], loadChildren: () => ... }