Angular中的嵌套路由出口
创始人
2024-10-31 08:31:22
0

在Angular中,可以通过嵌套路由出口来显示嵌套组件。下面是一个简单的示例,演示了如何在Angular中使用嵌套路由出口。

首先,在父组件的模板中,定义一个嵌套路由出口。可以使用元素来创建一个默认的路由出口,或者使用来创建一个具有特定名称的路由出口。


Parent Component

然后,在父组件的路由配置中,定义嵌套路由。可以使用children属性来定义嵌套的子路由数组。

// parent.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent { }
// parent-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { ParentComponent } from './parent.component';
import { Child1Component } from './child1.component';
import { Child2Component } from './child2.component';

const routes: Routes = [
  {
    path: '',
    component: ParentComponent,
    children: [
      { path: 'child1', component: Child1Component },
      { path: 'child2', component: Child2Component }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class ParentRoutingModule { }

接下来,创建子组件。子组件可以是任何普通的Angular组件。

// child1.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-child1',
  template: '

Child 1 Component

' }) export class Child1Component { }
// child2.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-child2',
  template: '

Child 2 Component

' }) export class Child2Component { }

最后,在路由模块中导入父组件和子组件,并配置路由。

// app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: '/parent', pathMatch: 'full' },
  { path: 'parent', loadChildren: () => import('./parent/parent.module').then(m => m.ParentModule) }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
// parent.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ParentRoutingModule } from './parent-routing.module';
import { ParentComponent } from './parent.component';
import { Child1Component } from './child1.component';
import { Child2Component } from './child2.component';

@NgModule({
  declarations: [
    ParentComponent,
    Child1Component,
    Child2Component
  ],
  imports: [
    CommonModule,
    ParentRoutingModule
  ]
})
export class ParentModule { }

通过以上步骤,现在可以在父组件的模板中使用嵌套路由出口了。路由器会根据路由配置自动加载相应的子组件,并在嵌套路由出口中显示。


Parent Component



在浏览器中导航到/parent/child1/parent/child2,就会在父组件的模板中显示相应的子组件。

相关内容

热门资讯

透视ai"wpk私人... 透视ai"wpk私人局辅助是真的吗"wepoker正确养号方法(竟然真的有辅助修改器)-哔哩哔哩1、...
透视新版"xpoke... 透视新版"xpoker透视辅助"xpoker怎么作必弊(总是是有辅助插件)-哔哩哔哩透视新版"xpo...
透视总结"hhpok... 透视总结"hhpoker可以控制牌吗"购买wepoker模拟器(竟然真的有辅助平台)-哔哩哔哩1、这...
现有说明如下"aap... 现有说明如下"aapoker万能辅助器"wepoker免费辅助器(都是是有辅助安装)-哔哩哔哩1、w...
据公告内容"aapo... 据公告内容"aapoker破解侠是真的吗"wepoker插件下载(一贯是真的辅助下载)-哔哩哔哩aa...
有消息称"hhpok... 有消息称"hhpoker脚本下载"哈糖大菠萝软件下载(切实是真的辅助辅助器)-哔哩哔哩1、全新机制【...
透视计算"wepok... 透视计算"wepoker辅助软件视频"hhpoker辅助靠谱吗(都是是有辅助神器)-哔哩哔哩1、we...
经调查"wepoke... 经调查"wepoker有辅助吗"菠萝德普辅助器免费版在哪里(一贯有辅助修改器)-哔哩哔哩1、任何菠萝...
透视透视"wepok... 透视透视"wepoker私人局开挂视频"hhpoker脚本下载(总是真的是有辅助工具)-哔哩哔哩小薇...
透视规律"wpk真的... 透视规律"wpk真的有透视嘛"wepoker游戏安装教程(其实存在有辅助神器)-哔哩哔哩1、在wep...