【问题标题】:Child routes are not detected in AngularAngular 中未检测到子路由
【发布时间】:2020-04-05 20:40:06
【问题描述】:

我正在开发一个离子应用程序 (Ionic 5),我似乎无法让路由正常运行。这是我的应用路线。

但我似乎无法让这个网址工作 /state/test-centers

Angular 没有找到那条路线.. 这是跟踪

我只是在尝试这个..我也尝试过这个..也没有用..

{
    path: "state",
    component: StateComponent,
    pathMatch: "full",
    children: [
      { path: "test-centers", component: TestCentersComponent, pathMatch: "full" }
    ]
  }

【问题讨论】:

  • 路由结构中的小重构可以吗?
  • 嗨@GytisTenovimas 请让我知道你想对我做什么。在我早期的应用程序中,我曾经在路由有多个子页面时创建模块,但由于这个应用程序更简单,所以我不想创建模块。我是否必须创建模块才能使子根正常工作?
  • 在下面查看我的答案。我认为这里的主要问题是pathMatch: 'full',因为您在网址中添加的不仅仅是/state
  • 为什么要用children作为children,里面的第一个children,用它的组件定义path:'test-centers',意思是object 2的第二个children应该替换为object的第一个children一个而不是重复..
  • @MostafaHarb 是否也没有工作。我也编辑了我的问题。

标签: angular ionic-framework ionic5


【解决方案1】:

你不需要pathMatch策略,你的路由应该是这样的

{
  path: "state",
  component: StateComponent,
   children: [
   path: "",
   component: StateComponent,
    children: [
     {path: "test-centers" , component:TestCentersComponent}
   ]
 ] 
}

【讨论】:

  • 我试过这个......但在这种情况下,加载状态组件而不是 testceter..只要我知道 Angular 的默认 url 匹配技术是前缀......所以一旦它获得状态在 url 中,它使用它而不是进一步寻找。
  • 你说得对,但是角度匹配所有路由段,所以如果 url 中有其他段,它不会在第一次匹配时停止,这个 url 可能会帮助angular.io/guide/router#child-route-configuration
【解决方案2】:

如果您想让TestCentersComponent 内容显示为StateComponent 的子项,则在StateComponent 的模板中包含<router-outlet> 并使用以下路由定义。您可以使用 url /state/test-centers 访问它,您将看到父组件与子组件模板一起呈现,而不是插座。

{
  path: 'state',
  component: StateComponent,
  children: [
    { path: 'test-centers', component: TestCentersComponent },
  ]
}

另一种方法是使用第三个组件StateContainerComponent,充当容器/页面/布局占位符,其中只有<router-outlet>。这种方法将渲染StateComponentTestCentersComponent,但不会像上述方法一样将两者都渲染为父子渲染。也可以通过网址/state/test-centers访问。

{
  path: 'state',
  component: StateContainerComponent,
  children: [
    { path: 'test-centers', component: TestCentersComponent },
    { path: '', component: StateComponent },
  ]
}

这是一个stackblitz demo 展示了这两种方法,并指出对于后者,我使用state-container 路线只是为了演示目的。

【讨论】:

    【解决方案3】:

    测试场景:

    {
        path: 'state',
        component: StateComponent,
        children: [
          {
            path: 'centers',
            pathMatch: 'full',
            component: TestCentersComponent,
          },
        ],
    },
    

    此外,您需要将其添加到您的 StateComponent:

    <router-outlet></router-outlet>
    

    您的路线中的小变化。我认为 pathMatch: 'full' 是不需要的,因为它匹配整条路线,但您添加 /test-centers 是不匹配的。

    另外,如果你不打算添加更多的第一级孩子,那么我认为可以将第二级孩子移动到第一级,因为你只添加了path: '',这并没有多大作用。

    【讨论】:

    • 我这样做了,但如果我没有指定完全匹配,它会加载状态组件。 :( 我认为 Angular 的默认匹配技术是前缀,所以每当它看到状态/中心时,它都会匹配“状态”路径并加载该组件。我快疯了。。
    • 什么意思?您尝试访问哪个 URL、您获得了什么以及您期望获得什么?
    • 我有两个组件,State 和 TestCenters,当我访问 localhost:8100/state => 它给了我 State 组件,当我访问 localhost:8100/state/test-centers => 它还加载 Statement 组件。我希望加载 TestCener 组件。
    • 好吧,我可能知道这个问题。检查我更新的答案。我认为您没有在 StateComponent 中添加 &lt;router-outlet&gt;&lt;/router-outlet&gt;
    • 我不想在状态组件中添加中心组件。我想更新整个视口(它基本上是一个离子页面)。所以我希望它被推送到根路由器插座中。无论如何,我留下了希望,而不是定义孩子,我只是在根级别定义所有路由......
    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 2022-11-10
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2017-01-01
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多