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模拟器辅助学习!wepo... WPK模拟器辅助学习!wepoker私人局透视教程,AApokerai多功能辅助方法1、下载好wep...
WPK多开辅助器办法!wepo... WPK多开辅助器办法!wepoker辅助器如何使用,红龙PoKerai多功能辅助器攻略1、玩家可以在...
WPK方法开挂教程书!wepo... WPK方法开挂教程书!wepoker免费透视,菠萝智星德州ai多功能开挂脚本暗藏猫腻,小编详细说明w...
WPK德州局辅助器诀窍!wep... WPK德州局辅助器诀窍!wepoker透视脚本免费,微扑克ai多功能辅助器工具1、很好的工具软件,可...
WPK方法辅助器经验!德普之星... WPK方法辅助器经验!德普之星透视辅助软件是真的吗,红龙扑克ai多功能辅助器脚本1、进入到德普之星透...
WPK方法透视项目!wpk作弊... WPK方法透视项目!wpk作弊,红龙PoKerai多功能透视神器1、首先打开wpk作弊辅助器下载最新...
WPK辅助透视挂方针!wepo... WPK辅助透视挂方针!wepoker插件功能辅助器,wpkai多功能透视挂插件1、wepoker插件...
WPK真假辅助器举措!wepo... WPK真假辅助器举措!wepoker怎么开辅助,aapokerai多功能辅助器技巧1、WPK真假辅助...
WPK安装辅助器手册!有哪些免... WPK安装辅助器手册!有哪些免费的wpk作弊码,德州扑克ai多功能辅助器神器1、有哪些免费的wpk作...
WPK私人局透视挂要领!wep... WPK私人局透视挂要领!wepoker免费辅助器,微扑克ai多功能透视挂教程1、每一步都需要思考,不...