Angular中的嵌套路由出口
创始人
2024-10-31 08:31:46
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,就会在父组件的模板中显示相应的子组件。

相关内容

热门资讯

安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
安装安卓应用时出现“Play ... 在安装安卓应用时出现“Play Protect 警告弹窗”的原因是Google Play Prote...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安卓系统怎么连不上carlif... 安卓系统无法连接CarLife的原因及解决方法随着智能手机的普及,CarLife这一车载互联功能为驾...
本地化字符串和默认值 本地化字符串是指将应用程序中的文本内容根据不同的语言和地区进行翻译和适配的过程。当应用程序需要显示不...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
windows安装系统退不出来... Windows安装系统退不出来的解决方法详解在电脑使用过程中,有时会遇到在安装Windows系统时无...
不匹配以value="... 解决方法一:使用正则表达式匹配可以使用正则表达式来匹配不以value="开头的字符串。示例如下:im...