简体   繁体   English

如何在动态加载的 Angular 模块中正确设置路由?

[英]How to set up routing properly in a dynamically loaded Angular module?

I have an app with a router outlet, in which I want to lazy-load modules with their own sub-routes.我有一个带有路由器插座的应用程序,我想在其中使用自己的子路由延迟加载模块。

This should give the routes:这应该给出路线:

/
/lazy1/child1
/lazy1/child2

in the root app module, I define some routes that will lazy-load the child module在根应用模块中,我定义了一些将延迟加载子模块的路由

export const routes: Routes = [
  {path:'', component: HelloComponent},
  {path: 'lazy1', loadChildren: () => import('./lazy1-module/index').then(mod => mod.Lazy1Module)}
];

and in the Lazy1Module, the following routes并在 Lazy1Module 中,以下路线

const routes: Routes = [
 {path: 'child1', component: Child1Component},
 {path: 'child2', component: Child2Component},
 {path: '**', redirectTo: 'child1'}
];

which i add with RouterModule.forChild(routes) as normal.我正常添加 RouterModule.forChild(routes) 。

This seems to work fine, and when I go to /lazy1/child1 this resolves fine.这似乎工作正常,当我去 /lazy1/child1 时,这解决得很好。

However, when i try to navigate between the childs i bump into trouble.然而,当我试图在孩子们之间导航时,我遇到了麻烦。 As the lazy-loaded module in it self should not know or care for routes outside itself, I would assume that I could navigate from child1 to child2 using由于它本身的延迟加载模块不应该知道或关心自身外部的路由,我假设我可以使用从 child1 导航到 child2

this.router.navigate(["child2"]);

This results in a 'route not found' error.这会导致“找不到路由”错误。

I can make it work by adding我可以通过添加使其工作

this.router.navigate(["child2"], { relativeTo: this.route.parent });

But this seems wrong.但这似乎是错误的。 also "../child2" does not work. “../child2”也不起作用。

Adding logging to the router, it seems that it adds a "/" in front of the route and starts to resolve it from root also disregarding any dots.向路由器添加日志记录,它似乎在路由前添加了一个“/”,并开始从 root 解析它,也不考虑任何点。

I'm using Angular 8.2.14我正在使用 Angular 8.2.14

Stackblitz example here: https://stackblitz.com/edit/angular-bqdchr这里的 Stackblitz 示例: https ://stackblitz.com/edit/angular-bqdchr

Have a look at this stackblitz:看看这个堆栈闪电战:

I think it's because your lazy routes don't know it's own root so you need to add:我认为这是因为你的懒惰路由不知道它自己的根所以你需要添加:

path:'', component: LazyComponent, children: [
  { path: 'child1', component: Child1Component },
  { path: 'child2', component: Child2Component }
]

You can navigate by absolute or relative paths.您可以通过绝对或相对路径导航。

this.router.navigate(['child2']);

is using an absolute path (because there is no relativeTo flag) and navigates to /child2 .正在使用绝对路径(因为没有relativeTo标志)并导航到/child2 You have no route defined for /child2 but for /lazy1/child2 .你有没有为定义的路由/child2/lazy1/child2

The console error is telling you that:控制台错误告诉您:

Error: Cannot match any routes.错误:无法匹配任何路由。 URL Segment: 'child2' URL 段:'child2'

It would work if you would rewrite it to:如果您将其重写为:

this.router.navigate(['lazy1', 'child2']);

Your working example is using a relative path:您的工作示例使用的是相对路径:

this.router.navigate(["child2"], { relativeTo: this.route.parent });

The parent of you current route is lazy .您当前路线的父级是lazy From there you're navigating to child2 which is a valid route ( /lazy/child2 ).从那里你导航到child2这是一条有效的路线( /lazy/child2 )。

See https://angular.io/api/router/Router#navigate for more information.有关更多信息,请参阅https://angular.io/api/router/Router#navigate The following could also be interesting: https://angular.io/guide/router#router-links以下内容也可能很有趣: https : //angular.io/guide/router#router-links

Your routes of lazy as:你的懒惰路线为:

const routes: Routes = [
  {path:'',children:[
    {path: 'child1', component: Child1Component},
    {path: 'child2', component: Child2Component}
  ]},
 {path: '**', redirectTo: 'child1'}
];

Then, you can do然后,你可以做

this.router.navigate(["child2"], { relativeTo: this.route.parent });
//or
this.router.navigate(["../child2"],{ relativeTo: this.route});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM