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

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

相关内容

热门资讯

透视辅助!来玩德州破解器,拱趴... 透视辅助!来玩德州破解器,拱趴大菠萝辅助神器,真是存在有挂(扑克教程)在进入拱趴大菠萝辅助神器辅助挂...
透视肯定!聚星ai辅助工具激活... 透视肯定!聚星ai辅助工具激活码,wpk安卓下载辅助(透视)确实真的是有挂(攻略方法);1、该软件可...
透视苹果版!werplan脚本... 透视苹果版!werplan脚本,哈糖大菠萝助手,其实有挂(微扑克教程)1、游戏颠覆性的策略玩法,独创...
透视线上!aapoker插件下... 透视线上!aapoker插件下载,wepoker免费钻石(透视)本然真的是有挂(系统教程);1、操作...
透视黑科技!来玩app破解,红... 透视黑科技!来玩app破解,红龙poker辅助工具,原来是真的有挂(总结教程);1、每一步都需要思考...
透视黑科技!wepoker辅助... 透视黑科技!wepoker辅助是真的吗,wepoker辅助是真的吗(透视)竟然是有挂(教你攻略)1、...
透视透视!pokermaste... 透视透视!pokermaster修改器,智星菠萝辅助,好像是真的有挂(技巧教程)1、全新机制【pok...
透视工具!hhpoker的辅助... 透视工具!hhpoker的辅助是真的吗,哈糖大菠萝软件下载(透视)一贯真的是有挂(插件教程)1、不需...
透视代打!拱趴大菠萝挂哪里,德... 透视代打!拱趴大菠萝挂哪里,德州真人透视脚本,一贯是真的有挂(透明挂教程)1、下载好德州真人透视脚本...
透视透视!hhpoker德州真... 透视透视!hhpoker德州真的假的,拱趴大菠萝万能挂(透视)往昔是真的有挂(必赢方法);1、hhp...