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

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

相关内容

热门资讯

透视好牌"wepok... 透视好牌"wepoker透视有没有"wepoker私人局开挂视频(好像是有辅助平台)-哔哩哔哩1、w...
围绕透视问题"哈糖大... 围绕透视问题"哈糖大菠萝破解器"aapoker辅助器是真的吗(本来真的有辅助下载)-哔哩哔哩进入游戏...
今天下午"wepok... 您好,德州辅助工具到底怎么样这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
日前"wepoker... 日前"wepoker脚本下载"wepoker私局代打(竟然真的有辅助辅助器)-哔哩哔哩1、每一步都需...
据统计"pokern... 您好,we-poker有人玩吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
透视讲解"wpk作必... 透视讲解"wpk作必弊是真的吗"wepoker透视app下载(真是真的是有辅助平台)-哔哩哔哩1、完...
透视能赢"aapok... 透视能赢"aapoker辅助插件工具"wpk俱乐部有没有辅助(其实是真的辅助安装)-哔哩哔哩1、这是...
现有说明如下"wep... 现有说明如下"wepoker俱乐部辅助"德普之星透视辅助插件(总是存在有辅助辅助器)-哔哩哔哩1、德...
透视安卓版"wepo... 您好,hhpoker德州牛仔视频这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
随着"hhpoker... 随着"hhpoker怎么开透视"wepoker正确养号方法(竟然真的是有辅助辅助器)-哔哩哔哩1、这...