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

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

相关内容

热门资讯

热点推荐!hhpoker是内部... 热点推荐!hhpoker是内部控制吗(辅助挂)都是是真的有挂(果真有挂辅助app)能透视中分为三种模...
技术分享!wpk透视工作室(辅... 技术分享!wpk透视工作室(辅助挂)果然是有挂(新版有挂辅助方法)1、首先打开辅助器下载最新版本,在...
揭秘几款!wejoker透视方... 揭秘几款!wejoker透视方法(辅助挂)确实是真的有挂(有挂方法辅助教程)1、每一步都需要思考,不...
终于清楚!hhpoker软件安... 终于清楚!hhpoker软件安装包(辅助挂)总是存在有挂(有挂详细辅助攻略)1、起透看视 辅助软件价...
今日百科!哈糖大菠萝怎么开挂(... 今日百科!哈糖大菠萝怎么开挂(辅助挂)切实真的是有挂(有挂教学辅助器)1、破解器简单,机器人操作,插...
揭秘!德扑圈透视(辅助挂)其实... 揭秘!德扑圈透视(辅助挂)其实真的是有挂(有挂方法辅助器)1. 选牌创建新账号,点击进入游戏后,点击...
技术分享!We poker辅助... 技术分享!We poker辅助器下载(辅助挂)一直有挂(有挂教学辅助教程)破解侠是真的助透视。每个模...
2分钟细说!hhpoker脚本... 2分钟细说!hhpoker脚本(辅助挂)好像真的是有挂(有挂攻略辅助插件)1、2分钟细说!hhpok...
六分钟了解!hhpoker免费... 六分钟了解!hhpoker免费透视脚本(辅助挂)原来是有挂(详细教程辅助方法)一、游戏安装教程牌型概...
今日科普!pokerworld... 今日科普!pokerworld修改器(辅助挂)好像是有挂(有挂解惑辅助教程)1、用户打开应用后不用登...