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)外... 黑科技挂黑科技(德州wpk)外挂透明挂辅助神器(智能ai)竟然真的是有挂(有挂讲解)-哔哩哔哩1、金...
黑科技工具(wpk德州ai机器... 黑科技工具(wpk德州ai机器人)外挂透明挂辅助AI(透视)其实有挂(有挂攻略)-哔哩哔哩暗藏猫腻,...
黑科技软件(aapker代理)... 黑科技软件(aapker代理)外挂透明挂辅助挂(智能ai)好像有挂(确实有挂)-哔哩哔哩1、下载好a...
黑科技安卓版(WPK内置)外挂... 黑科技安卓版(WPK内置)外挂透明挂辅助代打(软件透明挂)一直是真的有挂(讲解有挂)-哔哩哔哩1、W...
黑科技黑科技(约局互娱)外挂透... 黑科技黑科技(约局互娱)外挂透明挂辅助插件(ai代打)其实有挂(今日头条)-哔哩哔哩1、起透看视 约...
黑科技玄学(wpk神器)外挂透... 黑科技玄学(wpk神器)外挂透明挂辅助挂(软件透明挂)一贯是有挂(有挂规律)-哔哩哔哩;1)wpk神...
黑科技ai(Wepoke教程)... 黑科技ai(Wepoke教程)外挂透明挂辅助黑科技(ai代打)都是是真的有挂(有挂分析)-哔哩哔哩1...
黑科技黑科技(aapoker德... 黑科技黑科技(aapoker德州线上)外挂透明挂辅助代打(黑科技ai)本来真的是有挂(发现有挂)-哔...
黑科技最新(Wepoke开挂)... 黑科技最新(Wepoke开挂)外挂透明挂辅助ai代打(黑科技ai)原来真的有挂(有挂方式)-哔哩哔哩...
黑科技教学(aapoker软件... 黑科技教学(aapoker软件)外挂透明挂辅助挂(黑科技ai)果然是真的有挂(有挂透视)-哔哩哔哩;...