You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular嵌套子路由问题:深层组件未触发,需保留children结构解决

解决Angular嵌套路由不触发子组件的问题

你的问题核心是父组件ViewAccountComponent的模板中缺少<router-outlet>标签,Angular的嵌套路由组件必须通过父组件内的路由出口才能渲染出来,这是嵌套路由生效的必要条件。

具体解决步骤:

  1. 打开ViewAccountComponent的模板文件(比如view-account.component.html),添加<router-outlet>标签。这个标签会作为子路由组件(ViewUserComponent)的渲染容器。

示例模板代码:

<!-- ViewAccountComponent的内容 -->
<h2>账户详情:{{ accountname }}</h2>
<!-- 子路由组件会渲染在这里 -->
<router-outlet></router-outlet>
  1. 你的路由配置本身是正确的,不需要修改account-routing.module.ts中的嵌套结构。当前的路由配置:
const routes: Routes = [
  {
    path: '',
    children: [
      {
        path: '',
        component: AccountComponent,
      },
      {
        path: ':accountname',
        component: ViewAccountComponent,
        children: [
          {
            path: ':userid',
            component: ViewUserComponent
          }
        ]
      },
    ]
  }
];

完成上述修改后,访问account/testaccountName/123I时,ViewAccountComponent会先渲染,然后ViewUserComponent会在它内部的<router-outlet>位置加载出来,嵌套路由就能正常工作了。

内容的提问来源于stack exchange,提问作者user16405471

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 10:00:24