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会在命名为main的router-outlet中显示。

如果没有匹配的路由,会显示NotFoundComponent。

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

相关内容

热门资讯

WPK安装辅助妙招!wepok... WPK安装辅助妙招!wepoker模拟器哪个,WePoKerai多功能辅助脚本1、进入游戏-大厅左侧...
推荐十款!透视!多乐麻将ai透... 推荐十款!透视!多乐麻将ai透视多功能插件,其实有透视安装妙计;近期一款 AI 黑科技工具火爆出圈,...
WPK安装辅助指引!wepok... WPK安装辅助指引!wepoker有没有插件,wpkai多功能辅助方法1、wepoker有没有插件有...
玩家必看教程辅助!微乐浙江麻将... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
WPK下载透视手册!wepok... WPK下载透视手册!wepoker破解器有用吗,wpkai多功能透视插件1、下载好wepoker破解...
实操分享辅助挂!非常竞技ai辅... 您好,实操分享辅助挂!非常竞技ai辅助挂多功能插件,最初有辅助挂平台办法这款游戏可以开挂的,确实是有...
WPK官网辅助练习!德普之星透... WPK官网辅助练习!德普之星透视辅助软件激活码,AApokerai多功能辅助工具一、德普之星透视辅助...
一分钟了解辅助挂!约战荆门麻将... 约战荆门麻将AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断网...
WPK真假透视挂操作!有哪些免... WPK真假透视挂操作!有哪些免费的wpk作弊码,红龙PoKerai多功能透视挂工具1、有哪些免费的w...
细节方法辅助!聚游广东麻将ai... 很多人使用云端 AI 时,都担心聚游广东麻将、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技...