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

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

相关内容

热门资讯

2分钟细说!wepoker游戏... 1、2分钟细说!wepoker游戏的安装教程,wepoker透视脚本,必赢教程(有挂透明)2、进入游...
透视美元局!pokernow辅... 透视美元局!pokernow辅助控制(透视)原来是有挂(详细辅助微扑克教程);进入游戏-大厅左侧-新...
热门推荐!wepokerplu... 1、热门推荐!wepokerplus辅助,hhpkoer辅助器视频,总结教程(有挂神器)2、进入游戏...
透视美元局!悦扑克脚本(透视)... 透视美元局!悦扑克脚本(透视)都是是有挂(详细辅助安装教程);1、进入到黑科技之后,能看到左侧胜局榜...
2分钟细说!wepoker怎么... 自定义竞技联盟透视插件系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管...
透视最新!aapoker破解侠... 透视最新!aapoker破解侠是真的吗(透视)切实真的是有挂(详细辅助切实教程)1、超多福利:超高返...
透视智能ai!wepoker透... 透视智能ai!wepoker透视挂底牌(透视)都是真的有挂(详细辅助教你攻略);运wepoker透视...
一分钟秒懂!pokermast... 一分钟秒懂!pokermaster破解版,wepoker养号规律,2025新版教程(有挂教程);实战...
透视数据!aapoker辅助器... 透视数据!aapoker辅助器是真的吗(透视)切实是真的有挂(详细辅助扑克教程)1、金币登录送、破产...
每日必看!we poker辅助... 每日必看!we poker辅助器,hhpoker免费辅助器,必胜教程(有挂技巧);支持2-10人实时...