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,就会在父组件的模板中显示相应的子组件。

相关内容

热门资讯

透视总结"德普辅助器... 透视总结"德普辅助器怎么用"智星德州有脚本吗(好像存在有辅助修改器)-哔哩哔哩1、完成德普辅助器怎么...
透视美元局"德州透视... 透视美元局"德州透视竞技联盟"德普之星透视(都是存在有辅助平台)-哔哩哔哩1、这是跨平台的德州透视竞...
透视规律"hhpok... 透视规律"hhpoker辅助软件是真的么"wepoker的辅助器(原来真的是有辅助平台)-哔哩哔哩;...
透视神器"fishp... 透视神器"fishpoker透视底牌"wepoker有没有机器人(一直真的是有辅助app)-哔哩哔哩...
连日来"智星德州插件... 连日来"智星德州插件2024最新版"wepoker有没有机器人(一直真的是有辅助工具)-哔哩哔哩1、...
透视黑科技"fish... 透视黑科技"fishpoker透视底牌"wepoker底牌透视(果然真的有辅助工具)-哔哩哔哩1、f...
无独有偶"wepok... 无独有偶"wepoker辅助器安装包定制"aapoker透视脚本(真是是真的辅助平台)-哔哩哔哩we...
透视软件"wepok... 您好,wepoker如何设置透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
于此同时"aapok... 于此同时"aapoker怎么设置抽水"wpk辅助(一直真的是有辅助工具)-哔哩哔哩aapoker怎么...
透视透视"werpl... 透视透视"werplan脚本"hhpoker哪个俱乐部靠谱(果然真的有辅助插件)-哔哩哔哩运hhpo...