加载中的生成路径名@angular/路由器的孩子



我正在使用带有 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中提取新网址?
也许,有可能以某种方式通过某种超时来保持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: () => ... }

最新更新