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

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

相关内容

热门资讯

透视总结!wpk免费辅助,wp... 透视总结!wpk免费辅助,wpk辅助软件(透视)总是有透视神器(哔哩哔哩)1、wpk辅助软件公共底牌...
透视阶段!德州局透视脚本免费版... 透视阶段!德州局透视脚本免费版下载手机版,约局吧如何查看是否有挂(透视)好像有挂(哔哩哔哩)1、德州...
透视积累!hhpoker的辅助... 透视积累!hhpoker的辅助是真的吗,hhpoker真的假的(透视)真是真的是有透视器(哔哩哔哩)...
透视策略!wepoker有没有... 透视策略!wepoker有没有机器人,wepoker辅助器安装包(透视)切实有挂(哔哩哔哩)1、超多...
透视积累!hhpoker外开挂... 透视积累!hhpoker外开挂靠谱吗,wepoker软件安装包(透视)确实是真的脚本器(哔哩哔哩)1...
透视指南!hhpoker德州机... 透视指南!hhpoker德州机器人,wpk透视辅助靠谱吗(透视)本来是真的挂(哔哩哔哩)wpk透视辅...
透视秘籍!德普之星有辅助软件吗... 透视秘籍!德普之星有辅助软件吗,德扑圈透视挂(透视)一贯有脚本挂(哔哩哔哩)德普之星有辅助软件吗能透...
透视绝活!wepoker破解工... 透视绝活!wepoker破解工具,pokernow辅助工具(透视)都是有挂(哔哩哔哩)1、poker...
透视教程书!wpk透视辅助,w... 透视教程书!wpk透视辅助,wpk作必弊最怕三个东西(透视)果然是真的透视攻略(哔哩哔哩)1、任何w...
透视资料!aapoker怎么控... 透视资料!aapoker怎么控制牌,拱趴大菠萝自动计算机器人(透视)其实是真的挂(哔哩哔哩)1、玩家...