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手机插件(透视)边锋辅助(一直有辅助修改器)-哔哩哔哩一、边锋辅助可以开透视的定...
今日!wpk安卓下载辅助(透视... 今日!wpk安卓下载辅助(透视)德普之星私人局辅助(确实真的是有辅助修改器)-哔哩哔哩德普之星私人局...
这一问题亟待解决!hardro... 这一问题亟待解决!hardrock透视工具(透视)宁波同乐游辅助下载(一直是有辅助插件)-哔哩哔哩1...
透视计算!德扑之心免费透视(透... 透视计算!德扑之心免费透视(透视)决战卡五星辅助看牌器(切实真的是有辅助工具)-哔哩哔哩1、很好的工...
备受关注的!hhpoker哪个... 备受关注的!hhpoker哪个俱乐部靠谱(透视)开心赢三张自建房有挂吗(好像是真的辅助平台)-哔哩哔...
相较于以往!wepoker辅助... 相较于以往!wepoker辅助器(透视)新海玉楼茶苑辅助器(确实真的是有辅助修改器)-哔哩哔哩所有人...
据了解!wpk模拟器(透视)小... 据了解!wpk模拟器(透视)小二互娱辅助(一贯是有辅助工具)-哔哩哔哩1、该软件可以轻松地帮助玩家将...
现场直击!wepoker私人局... 现场直击!wepoker私人局规律(透视)新八戒怎么控制牌型(好像有辅助插件)-哔哩哔哩新八戒怎么控...
透视美元局!大菠萝手游辅助(透... 透视美元局!大菠萝手游辅助(透视)心悦踢坑神器软件下载(一直是有辅助工具)-哔哩哔哩该软件可以轻松地...
透视免费!we poker辅助... 透视免费!we poker辅助器下载(透视)填大坑辅助器攻略(一贯真的是有辅助辅助器)-哔哩哔哩1、...