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辅助软件价格这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
目前!wepoker怎么设置盖... 目前!wepoker怎么设置盖牌(透视)好像真的是有辅助工具(真是有挂)-哔哩哔哩1、许多玩家不知道...
透视中牌率!hhpoker辅助... 透视中牌率!hhpoker辅助器(透视)本来存在有辅助插件(有挂存在)-哔哩哔哩一、hhpoker辅...
现有关情况通报如下!德扑HHp... 现有关情况通报如下!德扑HHpoker有挂吗(透视)都是有辅助安装(有挂解密)-哔哩哔哩1、超多福利...
今年以来!hhpoker的辅助... 今年以来!hhpoker的辅助是真的吗(透视)切实有辅助平台(有挂教学)-哔哩哔哩1、起透看视 hh...
随着!菠萝德州透视脚本(透视)... 随着!菠萝德州透视脚本(透视)确实有辅助插件(有挂方针)-哔哩哔哩1、超多福利:超高返利,海量正版游...
透视好牌!wepoker免费辅... 透视好牌!wepoker免费辅助器(透视)一直是有辅助辅助器(有挂细节)-哔哩哔哩1.wepoker...
透视模拟器!wepoker私人... 透视模拟器!wepoker私人局俱乐部怎么进(透视)真是真的有辅助神器(有挂教学)-哔哩哔哩小薇(辅...
为切实保障!hhpoker有没... 为切实保障!hhpoker有没有外开挂(透视)一直是有辅助平台(有挂头条)-哔哩哔哩1、很好的工具软...
透视插件!wepoker透视破... 透视插件!wepoker透视破解版(透视)好像是有辅助修改器(有挂猫腻)-哔哩哔哩1、在wepoke...