Angular路由子路由需要放在/目录下。
创始人
2024-10-28 06:00:55
0

在Angular中,子路由需要放在父路由的组件中,并且父路由的路径应该以斜杠(/)开头。以下是一个包含代码示例的解决方法:

首先,在父组件的HTML模板中,添加一个标签,用于渲染子路由的组件。例如,假设父组件的路径为/parent


这是父组件

然后,在父组件的路由配置中,定义子路由。可以在children属性中配置子路由的路径和对应的组件。例如,假设有两个子路由分别位于/parent/child1/parent/child2

// parent-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { ParentComponent } from './parent.component';
import { Child1Component } from './child1.component';
import { Child2Component } from './child2.component';

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

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class ParentRoutingModule { }

最后,在父组件的模块中导入并添加ParentRoutingModuleimports数组中:

// parent.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ParentComponent } from './parent.component';
import { Child1Component } from './child1.component';
import { Child2Component } from './child2.component';
import { ParentRoutingModule } from './parent-routing.module';

@NgModule({
  declarations: [
    ParentComponent,
    Child1Component,
    Child2Component
  ],
  imports: [
    CommonModule,
    ParentRoutingModule
  ]
})
export class ParentModule { }

现在,当访问/parent路径时,父组件将会被渲染,并且子组件的内容将会在标签中进行渲染。例如,当访问/parent/child1时,Child1Component将会被渲染在父组件中。

希望以上解决方法能对你有所帮助!

相关内容

热门资讯

9分钟下载!微乐小程序黑科技,... 9分钟下载!微乐小程序黑科技,提高微乐运气的方法(开挂)确实是有挂(2026)9分钟下载!微乐小程序...
透视工具!微乐小程序黑科技,微... 透视工具!微乐小程序黑科技,微乐贵阳捉鸡麻将辅助(透视)都是有挂(哔哩哔哩)1、首先打开微乐贵阳捉鸡...
一分钟开挂!微乐小程序黑科技,... 一分钟开挂!微乐小程序黑科技,微乐游戏竞技团辅助(开挂)其实是真的挂(2026)1、在微乐小程序黑科...
透视玄学!微乐小程序黑科技,微... 透视玄学!微乐小程序黑科技,微乐自建房怎么提高运气(透视)一直存在有挂(哔哩哔哩)1、这是跨平台的微...
第3分钟脚本!微乐小程序免费黑... 第3分钟脚本!微乐小程序免费黑科技,微信小程序微乐游戏辅助(开挂)一贯是真的挂(2026)1、首先打...
教学辅助挂!微乐小程序免费黑科... 教学辅助挂!微乐小程序免费黑科技,2025微乐小程序黑科技(透视)本来是真的挂(哔哩哔哩)微乐小程序...
第3分钟神器!微乐小程序黑科技... 第3分钟神器!微乐小程序黑科技,微乐自建房插件如何安装(开挂)竟然真的有挂(2026)1.微乐小程序...
第三方辅助挂!微乐小程序黑科技... 第三方辅助挂!微乐小程序黑科技,微乐自建房辅助入口官网(透视)一直是真的挂(哔哩哔哩)1、全新机制【...
4分钟开挂!微乐小程序黑科技,... 4分钟开挂!微乐小程序黑科技,微乐小程序透视工具(开挂)原来有挂(2026);1、首先打开微乐小程序...
透视好友房!微乐小程序黑科技,... 透视好友房!微乐小程序黑科技,微信微乐辅助插件(透视)原来真的是有挂(哔哩哔哩)1、微信微乐辅助插件...