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

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

相关内容

热门资讯

查到实测辅助!微信卡五星辅助器... 查到实测辅助!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)教程1、超多福利:超高返利...
传递经验!微乐自建房辅助工具挂... 传递经验!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)技巧1、操作简单,无需微乐自建房辅...
一分钟教你!微信卡五星辅助器,... 一分钟教你!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)插件1、这是跨平台的微乐家乡...
传递经验!微乐小程序自建房辅助... 传递经验!微乐小程序自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)脚本传递经验!微乐小程序...
玩家实测!微乐自建房辅助工具挂... 玩家实测!微乐自建房辅助工具挂,微乐自建房脚本免费入口(开挂自建房)工具1、操作简单,无需微乐自建房...
推荐一款!微信微乐自建房辅助工... 推荐一款!微信微乐自建房辅助工具,微乐小程序免费黑科技(开挂自建房)攻略1、许多玩家不知道微信微乐自...
今日公布!微乐自建房辅助工具免... 今日公布!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)工具;1、任何微乐自建房...
必看攻略!微乐自建房辅助工具挂... 必看攻略!微乐自建房辅助工具挂,微乐自建房脚本免费入口(开挂自建房)脚本亲,关键说明,微乐自建房辅助...
科普常识!微乐微信自建房辅助工... 科普常识!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)教程1、完成微乐微信...
揭秘几款!微乐自建房辅助工具插... 揭秘几款!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)脚本一、微乐家乡麻将自建...