繁体   English   中英

错误:无法匹配任何路由。 懒加载一个Angular辅助路由时

[英]Error: Cannot match any routes. When lazy loading an Angular auxilary route

我正在尝试进行嵌套子路由调用以加载到辅助路由器插座中,但我似乎无法使其工作。 我不断收到Error: Cannot match any routes. URL Segment:'header/secondary/abc' Error: Cannot match any routes. URL Segment:'header/secondary/abc'

StackBlitz 链接: https://stackblitz.com/edit/angular-ivy-t3x2cw?file=src/app/header/header.component.ts

我的预期结果是在普通路由器出口<router-outlet></router-outlet>的左侧加载辅助和 Abc 模块/组件,并在辅助路由<router-outlet name="aux"></router-outlet>的右侧加载测试组件<router-outlet name="aux"></router-outlet> 如下图所示。 试图在 ABC 组件右侧的辅助路径中加载 Test 组件

有几个问题:

首先,注意header.component.html的内容:

<div>
  <router-outlet></router-outlet>
</div>
<div>
  <router-outlet name="aux"></router-outlet>
</div>

header组件的路线:

const routes: Routes = [
  {
    path: '',
    component: HeaderComponent,
    children: [
      { path: '', redirectTo: 'secondary', pathMatch: 'full' },
      {
        path: 'secondary',
        loadChildren: () =>
          import('./../secondary/secondary.module').then(
            (m) => m.SecondaryModule
          ),
      },
    ],
  },
];

组件视图想要显示的内容与路由配置描述的内容不匹配。 根据经验,组件X视图中的内容必须与组件X在路由配置中所需的内容相对应。 在这种情况下, header组件的视图需要一个命名的 outletaux ,但在路由配置中只有主要出口的路径(即secondary出口)。

因此,如果您希望您的组件处理多个插座,您可以执行以下操作:

// header.component route configuration

const routes: Routes = [
  {
    path: '',
    component: HeaderComponent,
    children: [
      { path: '', redirectTo: 'secondary', pathMatch: 'full' },
      {
        path: 'secondary',
        loadChildren: () =>
          import('./../secondary/secondary.module').then(
            (m) => m.SecondaryModule
          ),
      },
      
      // !
      {
        path: 'foo',
        outlet: 'aux',
        component: FooComponent,
      },
    ],
  },
];

并且navigate()方法看起来像这样:

navigate() {
    this.router.navigate([
      "header",
      {
        // key-value pairs
        // `key`: the **name** of the outlet
        // `value`: the paths defined for the outlet
        outlets: {
          primary: ["secondary", "abc"],
          aux: ["foo"]
        }
      }
    ]);
  }

StackBlitz 演示

此外,如果您想了解更多关于 Angular 路由器的信息,我建议您查看:

暂无
暂无

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

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