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

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

相关内容

热门资讯

每日必看!红龙poker辅助挂... 每日必看!红龙poker辅助挂多功能透视工具,切实真的有挂(有挂细节)1、红龙poker有没有辅助教...
一分钟揭秘!红龙poker辅助... 一分钟揭秘!红龙poker辅助挂多功能透视工具,果然存在有挂(有挂总结)该软件可以轻松地帮助玩家将红...
重大通报!微扑克辅助挂多功能透... 重大通报!微扑克辅助挂多功能透视工具,一直存在有挂(有挂教程)1)微扑克辅助插件:进一步探索微扑克辅...
玩家必看教程!德普之星辅助挂多... 玩家必看教程!德普之星辅助挂多功能透视工具,竟然是有挂(有挂方式)1、在德普之星插件功能辅助器技巧中...
技术分享!wepoker辅助挂... 技术分享!wepoker辅助挂多功能透视工具,切实真的有挂(有挂解惑)1、每一步都需要思考,不同水平...
热门推荐!德扑之星辅助挂多功能... 热门推荐!德扑之星辅助挂多功能透视工具,确实存在有挂(确实有挂)1、不需要AI权限,帮助你快速的进行...
玩家必看教程!AAPoKer辅... 玩家必看教程!AAPoKer辅助挂多功能透视工具,切实有挂(果真有挂)1、玩家可以在AAPoKer线...
记者爆料!智星德州扑克辅助挂多... 记者爆料!智星德州扑克辅助挂多功能透视工具,好像有挂(有挂秘诀)进入游戏-大厅左侧-新手福利-激活码...
揭秘几款!德扑之星辅助挂多功能... 揭秘几款!德扑之星辅助挂多功能透视工具,确实有挂(有挂方式)德扑之星透视方法中分为三种模型:德扑之星...
揭秘真相!微扑克辅助挂多功能透... 揭秘真相!微扑克辅助挂多功能透视工具,切实是有挂(有挂秘诀)1、微扑克辅助器安装包、微扑克辅助器2、...