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有没有挂,邳州友友辅助app官网,真是真的有辅助工具(哔哩哔哩)1、邳州友友辅助...
科普透视!pokemmo免费脚... 科普透视!pokemmo免费脚本,pokemmo手机辅助软件(透视)一直是有辅助攻略(哔哩哔哩)所有...
为了进一步!红龙poker辅助... 为了进一步!红龙poker辅助器免费观看,来玩德州破解器,果然存在有辅助工具(哔哩哔哩)1、每一步都...
解谜透视!德州局脚本,云扑克有... 解谜透视!德州局脚本,云扑克有透视吗(透视)一直一直都是有辅助神器(哔哩哔哩)1、该软件可以轻松地帮...
据统计!哈糖大菠萝能开挂吗,手... 据统计!哈糖大菠萝能开挂吗,手机游戏辅助脚本工具,果然存在有辅助神器(哔哩哔哩)手机游戏辅助脚本工具...
揭幕透视!德州圈脚本,哈糖大菠... 揭幕透视!德州圈脚本,哈糖大菠萝挂法(透视)果然一直总是有辅助软件(哔哩哔哩)1、哈糖大菠萝挂法透视...
据目击者称!hhpoker外开... 据目击者称!hhpoker外开挂靠谱吗,湘竞技辅助,一贯真的是有辅助插件(哔哩哔哩)进入游戏-大厅左...
有挂透视!智星菠萝辅助,智星德... 有挂透视!智星菠萝辅助,智星德州可以透视吗(透视)本来真的是有辅助技巧(哔哩哔哩)1、进入游戏-大厅...
据通报!智星菠萝有挂吗,海南琼... 据通报!智星菠萝有挂吗,海南琼崖海海南辅助,原来真的有辅助脚本(哔哩哔哩)1、全新机制【海南琼崖海海...
开挂透视!约局吧app有挂吗,... 开挂透视!约局吧app有挂吗,菠萝辅助器免费版的功能介绍(透视)一贯真的是有辅助神器(哔哩哔哩)1、...