【发布时间】:2020-01-22 13:49:34
【问题描述】:
我有一个带有路由器插座的应用程序,我想在其中延迟加载带有自己子路由的模块。
这应该给出路线:
/
/lazy1/child1
/lazy1/child2
在根应用模块中,我定义了一些将延迟加载子模块的路由
export const routes: Routes = [
{path:'', component: HelloComponent},
{path: 'lazy1', loadChildren: () => import('./lazy1-module/index').then(mod => mod.Lazy1Module)}
];
在 Lazy1Module 中,以下路线
const routes: Routes = [
{path: 'child1', component: Child1Component},
{path: 'child2', component: Child2Component},
{path: '**', redirectTo: 'child1'}
];
我像往常一样使用 RouterModule.forChild(routes) 添加。
这似乎工作正常,当我转到 /lazy1/child1 时,这解决得很好。
但是,当我尝试在孩子之间导航时,我遇到了麻烦。 由于其自身的延迟加载模块不应该知道或关心自身外部的路由,我假设我可以使用
从 child1 导航到 child2this.router.navigate(["child2"]);
这会导致“找不到路由”错误。
我可以通过添加使其工作
this.router.navigate(["child2"], { relativeTo: this.route.parent });
但这似乎是错误的。 “../child2”也不起作用。
向路由器添加日志记录,似乎它在路由前面添加了一个“/”并开始从根目录解析它,也忽略任何点。
我正在使用 Angular 8.2.14
此处为 Stackblitz 示例:https://stackblitz.com/edit/angular-bqdchr
【问题讨论】:
-
this.router.navigate(['/lazy/child2']); -
感谢您的评论,但是我尽量不使用绝对路径,因为延迟加载的模块可能会在不同的上下文中加载,并且不一定将“延迟”作为父级。