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

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

相关内容

热门资讯

透视总结!wepoker透视版... 透视总结!wepoker透视版下载,aapoker辅助包,揭秘一下(有挂总结)1、完成aapoker...
wepoke黑科技!wepok... wepoke黑科技!wepoke辅助有挂,(wEpOke)果然真的是有挂,切实教程(有挂工具);1、...
透视好牌!aapkoer德州辅... 透视好牌!aapkoer德州辅助挂下载,德普之星辅助功能如何打开,程序员教你(有挂透明)1、全新机制...
aapoker透明挂!aapo... aapoker透明挂!aapoker外挂,(wepoker)都是真的是有挂,攻略教程(有挂脚本)1、...
透视能赢!wepoker辅助透... 透视能赢!wepoker辅助透视软件,wepoker透视脚本,推荐十款(有挂规律)1、打开软件启动之...
aapoker猫腻!德扑之星有... aapoker猫腻!德扑之星有作弊,(wEpOke)切实是真的有挂,2025新版总结(有挂黑科技)1...
透视黑科技!wpk辅助器安装,... 透视黑科技!wpk辅助器安装,wepoker脚本,分享个大家(有挂教学);1、点击下载安装,插件透视...
德州ai辅助软件!wepoke... 德州ai辅助软件!wepoke辅助技巧,(wePoke)确实真的有挂,存在挂教程(有挂攻略)1、打开...
透视智能ai!德普之星辅助正版... 透视智能ai!德普之星辅助正版,aapoker透视脚本,发现玩家(有挂透明);1、透视智能ai!德普...
wpk有外挂!微扑克ai辅助,... wpk有外挂!微扑克ai辅助,(wEPOKE)好像是有挂,必赢教程(有挂解密);1、每一步都需要思考...