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透视有没有"详细辅助透明挂教程(总是真的是有挂);《WPK辅助透视》‌:支持...
透视玄学"wepok... 1、透视玄学"wepokerplus万能挂"详细辅助介绍教程(本然真的是有挂);详细教程。2、wep...
透视中牌率"wepo... 透视中牌率"wepoker作弊视频"详细辅助德州论坛(一贯有挂)关于wepoker作弊视频机制的,其...
透视脚本"wepok... 透视脚本"wepoker有插件吗"详细辅助解说技巧(最初真的有挂);1分钟了解详细教程(微信 136...
透视软件"hhpok... 透视软件"hhpoker软件靠谱吗"详细辅助可靠教程(固有是真的有挂)准备好在hhpoker软件靠谱...
透视神器"hh po... 透视神器"hh poker辅助器先试用"详细辅助新2025版(果然存在有挂)科技教程也叫必备教程,这...
透视软件"哈糖大菠萝... 透视软件"哈糖大菠萝有没有挂"详细辅助黑科技教程(原先是真的有挂)是一款可以让一直输的玩家,快速成为...
透视透视挂"约局吧怎... 透视透视挂"约局吧怎么看有没有挂"详细辅助2025新版技巧(本然有挂);最新版2024是一款经典耐玩...
透视脚本"约局吧德州... 透视脚本"约局吧德州有挂吗"详细辅助详细教程(好像是有挂)是一款可以让一直输的玩家,快速成为一个“必...
透视苹果版"wepo... 透视苹果版"wepoker怎么买辅助"详细辅助攻略方法(素来是有挂);玩家必备必赢加哟《136704...