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免费脚本咨询"一贯是真的辅助脚本(哔哩哔哩)1、很好的工具软件,可以解锁游戏...
透视揭露"aapok... 透视揭露"aapoker ai插件"竟然真的是有辅助器(哔哩哔哩)1、操作简单,无需aapoker ...
透视关于"aapok... 透视关于"aapoker透视怎么用"其实真的是有辅助神器(哔哩哔哩)1、aapoker透视怎么用免费...
透视了解"德普之星的... 透视了解"德普之星的辅助工具介绍"一直存在有辅助软件(哔哩哔哩)暗藏猫腻,小编详细说明德普之星的辅助...
透视解谜"wepok... 透视解谜"wepoker数据分析"果然真的是有辅助方法(哔哩哔哩)1、游戏颠覆性的策略玩法,独创攻略...
透视开挂"hhpko... 透视开挂"hhpkoer辅助器视频"切实是真的辅助攻略(哔哩哔哩)该软件可以轻松地帮助玩家将hhpk...
透视详细"wepok... 透视详细"wepoker究竟有没有透视"确实是有辅助app(哔哩哔哩)1、wepoker究竟有没有透...
透视解谜"we-po... 透视解谜"we-poker有人玩吗"确实是有辅助神器(哔哩哔哩)1、we-poker有人玩吗脚本辅助...
透视揭幕"红龙pok... 透视揭幕"红龙poker辅助平台"好像有辅助挂(哔哩哔哩)1、红龙poker辅助平台免费脚本咨询教程...
透视透视"wpk模拟... 透视透视"wpk模拟器多开"其实存在有辅助软件(哔哩哔哩)1、任何wpk模拟器多开透视是真的假的的玩...