Angular命名的路由出口不如预期工作
创始人
2024-10-28 13:01:01
0

案例代码如下,其中app-routing.module.ts是Angular的路由模块文件,app.component.html是应用的根组件模板文件。

app-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { NotFoundComponent } from './not-found/not-found.component';

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent, outlet: 'main' },
  { path: 'about', component: AboutComponent, outlet: 'main' },
  { path: '**', component: NotFoundComponent }
];

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

app.component.html:



在上面的代码中,我们定义了两个router-outlet,一个是默认的出口,另一个是命名为main的出口。路由配置中的outlet属性指定了每个路由对应的出口。

在模板文件app.component.html中,我们使用了两个router-outlet,分别用于显示默认的路由和命名为main的路由。

这样,当路由匹配到home路径时,HomeComponent会在默认的router-outlet中显示,当路由匹配到about路径时,AboutComponent会在命名为mainrouter-outlet中显示。

如果没有匹配的路由,会显示NotFoundComponent

这样,我们就可以根据需要在不同的路由出口中显示不同的组件。

相关内容

热门资讯

记者获悉!wepoker的辅助... 记者获悉!wepoker的辅助器,新星游挂多少钱,原来真的是有辅助插件(哔哩哔哩)1、每一步都需要思...
长期以来!wepoker破解游... 长期以来!wepoker破解游戏盒子,闲来潮汕辅助,真是是有辅助攻略(哔哩哔哩)在进入闲来潮汕辅助软...
网友热议!德普之星透视,蛮王大... 网友热议!德普之星透视,蛮王大厅透视辅助,其实真的有辅助app(哔哩哔哩)1、下载好蛮王大厅透视辅助...
为切实保障!来玩app破解版,... 为切实保障!来玩app破解版,约战竞技场辅助脚本,真是真的是有辅助教程(哔哩哔哩)1、约战竞技场辅助...
据相关数据显示!wepoker... 据相关数据显示!wepoker游戏下载,九酷众游辅助,切实存在有辅助攻略(哔哩哔哩)所有人都在同一条...
今天上午!epoker底牌透视... 今天上午!epoker底牌透视,德州之星插件,好像真的有辅助技巧(哔哩哔哩)1、用户打开应用后不用登...
备受关注的!wepoker辅助... 备受关注的!wepoker辅助器怎么弄,随意玩聚乐部辅助,原来有辅助工具(哔哩哔哩)一、随意玩聚乐部...
关于透视!pokemomo辅助... 关于透视!pokemomo辅助软件,pokermaster脚本(透视)本来存在有辅助攻略(哔哩哔哩)...
此事备受玩家关注!WePoKe... 此事备受玩家关注!WePoKer辅助器,游戏浙江大厅脚本修改,总是存在有辅助app(哔哩哔哩)1、游...
曝光透视!约局吧怎么看有没有挂... 曝光透视!约局吧怎么看有没有挂,德州透视脚本(透视)真是是真的有辅助软件(哔哩哔哩)一、约局吧怎么看...