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

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

相关内容

热门资讯

普及透视!wpk模拟器是什么,... 普及透视!wpk模拟器是什么,德普辅助器怎么用,资料教程(有挂透明挂)-哔哩哔哩1)德普辅助器怎么用...
专业透视!约局吧开挂!本来是有... 专业透视!约局吧开挂!本来是有辅助技巧(有挂分享)-哔哩哔哩约局吧开挂是不是有人用挂微扑克wpk插件...
解迷透视!佛手在线大菠萝为什么... 解迷透视!佛手在线大菠萝为什么都输!本来是有辅助神器(有挂功能)-哔哩哔哩1、玩家可以在佛手在线大菠...
详情透视!wpk俱乐部辅助器,... 详情透视!wpk俱乐部辅助器,德州局怎么透视,举措教程(讲解有挂)-哔哩哔哩1、wpk俱乐部辅助器免...
揭幕透视!智星菠萝可以辅助吗!... 揭幕透视!智星菠萝可以辅助吗!其实是真的有辅助教程(真的有挂)-哔哩哔哩1、这是跨平台的智星菠萝可以...
开挂透视!wepoker底牌透... 开挂透视!wepoker底牌透视,wepoker辅助软件视频,教材教程(有挂讲解)-哔哩哔哩1、we...
必备透视!xpoker辅助控制... 必备透视!xpoker辅助控制!确实是真的有辅助软件(有挂教程)-哔哩哔哩1、完成xpoker辅助控...
详情透视!hh poker辅助... 详情透视!hh poker辅助有用吗,wepoker辅助透视软件,练习教程(确实有挂)-哔哩哔哩1、...
解迷透视!哈糖大菠萝能开挂吗!... 解迷透视!哈糖大菠萝能开挂吗!一贯真的是有辅助app(存在有挂)-哔哩哔哩一、哈糖大菠萝能开挂吗游戏...
科普透视!poker worl... 科普透视!poker world辅助,wepoker亲友圈有用吗,课程教程(有挂详情)-哔哩哔哩1、...