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

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

相关内容

热门资讯

热点推荐!pokerist有挂... 热点推荐!pokerist有挂(辅助挂)软件透明挂(有挂介绍)-哔哩哔哩;pokerist有挂黑科技...
玩家必用(Wepoke输赢)外... 玩家必用(Wepoke输赢)外挂透明挂辅助器安装(透视)原来真的有挂(透明挂教程)(哔哩哔哩);We...
技巧知识分享!德扑ai代打会检... 德扑ai代打会检测到赢率提升策略‌;技巧知识分享!德扑ai代打会检测到(辅助挂)透视辅助(有挂总结)...
透视辅助(wpk安装)外挂透明... 透视辅助(wpk安装)外挂透明挂辅助挂(辅助挂)确实真的有挂(2024新版总结)(哔哩哔哩);玩家在...
一分钟揭秘(wpk安卓)外挂透... 一分钟揭秘(wpk安卓)外挂透明挂辅助机制(辅助挂)德州ai机器人(有挂技巧)-哔哩哔哩;揭秘教程安...
一秒答解!德州ai人工智能软件... 一秒答解!德州ai人工智能软件免费(透视)辅助透视(有挂教学)-哔哩哔哩;亲们利用一分钟了解精致的软...
揭秘关于(fish poker... 【福星临门,好运相随】;揭秘关于(fish poker外挂)外挂透明挂辅助软件(透视)软件透明挂(2...
玩家必看(Wepoke总结)外... WePoke高级策略深度解析‌;玩家必看(Wepoke总结)外挂透明挂辅助挂(透视)的确是真的有挂(...
玩家必看(微扑克线上)外挂透明... 【福星临门,好运相随】;玩家必看(微扑克线上)外挂透明挂辅助器安装(透视)德州ai机器人(确实有挂)...
玩家必备科普!epoker外挂... 玩家必备科普!epoker外挂(辅助挂)透视辅助(有挂详情)-哔哩哔哩;人气非常高,ai更新快且高清...