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将会被渲染在父组件中。

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

相关内容

热门资讯

第5分钟辅助!老友广东潮汕麻雀... 老友广东潮汕麻雀辅助是一款专注玩家量身打造的游戏记牌类型软件,在老友广东潮汕麻雀辅助这款游戏中我们可...
7分钟科普!盛世辅助器(辅助挂... 7分钟科普!盛世辅助器(辅助挂)其实真的是有挂(wepoke教程开挂辅助平台) 了解更多开挂安装加(...
第七分钟开挂!温州茶苑辅助器(... 第七分钟开挂!温州茶苑辅助器(辅助挂)原来真的是有挂(靠谱教程开挂辅助插件)>>您好:软件加薇136...
4分钟辅助!微友助手破解版(辅... 4分钟辅助!微友助手破解版(辅助挂)果然是有挂(微扑克教程开挂辅助插件);亲,微友助手破解版这款游戏...
6分钟科普!新518互游脚本(... 6分钟科普!新518互游脚本(辅助挂)其实是有挂的(透牌教程开挂辅助平台)《详细加薇13670430...
第四分钟辅助!新九方透视挂(辅... 第四分钟辅助!新九方透视挂(辅助挂)其实真的有挂(wpk教程开挂辅助下载);亲,新九方透视挂这款游戏...
第8分钟明白!奇迹陕西靖边锅子... 第8分钟明白!奇迹陕西靖边锅子有没有技巧(辅助挂)一贯有挂(教你攻略开挂辅助安装)>>您好:软件加1...
十分钟透视!八闽福建辅助(辅助... 十分钟透视!八闽福建辅助(辅助挂)果然真的是有挂(细节揭秘开挂辅助插件);无需打开直接搜索薇:136...
第九分钟透视!顺欣茶楼辅助软件... 第九分钟透视!顺欣茶楼辅助软件(辅助挂)果然是有挂(介绍教程开挂辅助安装);无需打开直接搜索加薇13...
第2分钟科普!逍遥辅助下载地址... 第2分钟科普!逍遥辅助下载地址(辅助挂)果然真的是有挂(普及教程开挂辅助安装)您好:逍遥辅助下载地址...