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

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

相关内容

热门资讯

教学盘点!wpk可以辅助吗,w... 教学盘点!wpk可以辅助吗,wepoker有机器人吗,必赢方法(一贯确实有挂)-哔哩哔哩;wepok...
玩家必看科普!德普之星透视辅助... 玩家必看科普!德普之星透视辅助软件,hhpoker辅助挂下载,我来教教你(其实是有挂的)-哔哩哔哩;...
透视有挂!wejoker辅助脚... 透视有挂!wejoker辅助脚本,we poker辅助器v3.3,细节揭秘(一直确实有挂)-哔哩哔哩...
重大通报!wepoker亲友圈... 重大通报!wepoker亲友圈有用吗,wpk俱乐部怎么辅助,规律教程(果然是有挂的)-哔哩哔哩;亲真...
必备科技!德普之星有辅助软件吗... 必备科技!德普之星有辅助软件吗,hhpoker是内部控制吗,详细教程!(一直确实有挂)-哔哩哔哩;人...
揭秘关于!德州局hhpoker... 揭秘关于!德州局hhpoker,wepoker有人用过吗,线上教程(一贯真的有挂)-哔哩哔哩;超受欢...
必备辅助推荐!wepoker破... 《必备辅助推荐!wepoker破解游戏盒子,aapoker插件下载,揭秘教程(原来有挂)-哔哩哔哩》...
我来分享!wepoker私人局... 我来分享!wepoker私人局透视插件,wepoker轻量版有透视吗,2026版教程(原来真的有挂)...
透视规律!aapoker如何设... 透视规律!aapoker如何设置胜率,德州透视竞技联盟,2026教程(原来是有挂的)-哔哩哔哩;1、...
实测教程!哈糖大菠萝有挂吗5个... 实测教程!哈糖大菠萝有挂吗5个常用方法,pokeplus脚本,黑科技教程(一直真的是有挂)-哔哩哔哩...