Angular中的(次要)命名路由是如何工作的?
创始人
2024-10-31 02:00:13
0

在Angular中,次要命名路由是一种可以在同一页面上同时加载多个组件的方法。当用户导航到次要命名路由时,它将在当前页面上显示一个新的组件,而不是导航到一个完全不同的页面。

以下是一个示例,展示了如何在Angular中使用次要命名路由:

  1. 首先,在路由配置文件(通常是app-routing.module.ts)中定义次要命名路由。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';
import { ContactComponent } from './contact.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about', component: AboutComponent, outlet: 'secondary' },
  { path: 'contact', component: ContactComponent, outlet: 'secondary' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
  1. 在模板文件(通常是app.component.html)中添加一个用于加载次要命名路由的占位符:





  1. 在主要命名路由组件的模板中添加一个用于导航到次要命名路由的链接:
About
Contact
  1. 最后,在主要命名路由组件的对应的ts文件中,使用Router对象进行导航:
import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-home',
  template: `
    

Home

` }) export class HomeComponent { constructor(private router: Router) {} goToAbout() { this.router.navigate([{ outlets: { secondary: ['about'] } }]); } goToContact() { this.router.navigate([{ outlets: { secondary: ['contact'] } }]); } }

现在,当用户点击“About”按钮时,AboutComponent将会在当前页面上显示。当用户点击“Contact”按钮时,ContactComponent将会在当前页面上显示。

这就是在Angular中使用次要命名路由的基本方法。你可以根据需要进行调整和扩展。

相关内容

热门资讯

wepoker的辅助器!wep... wepoker的辅助器!wepokerplus透视(透视)插件-切实透视有挂wepoker的辅助器辅...
wepoker透视有用吗!we... wepoker透视有用吗!we poker插件(透视)攻略-果然关于真的是有挂1、上手简单,内置详细...
德普之星app安卓版破解版!w... 德普之星app安卓版破解版!wepoker免费脚本咨询(透视)攻略-确实解迷真的是有挂德普之星app...
wepoker免费脚本咨询!h... wepoker免费脚本咨询!hhpoker德州挂真的有吗(透视)方法-都是推荐有挂小薇(辅助器软件下...
pokeplus脚本!德州局透... pokeplus脚本!德州局透视(透视)神器-真是了解真的有挂1)德州局透视免费钻石:进一步探索德州...
wepoker买钻石有用吗!w... wepoker买钻石有用吗!wpk私人局有透视吗(透视)工具-好像科普真的是有挂1、起透看视 wep...
wepoker怎么设置透视!德... wepoker怎么设置透视!德普之星app安卓版破解版(透视)教程-总是总结真的有挂1、不需要AI权...
wepoker养号规律!大菠萝... wepoker养号规律!大菠萝手游辅助(透视)方法-原来必备是真的挂1、用户打开应用后不用登录就可以...
哈糖大菠萝怎么挂!wepoke... 哈糖大菠萝怎么挂!wepoker有透视底牌吗(透视)挂-一贯辅助真的是有挂;1、金币登录送、破产送、...
wepokerplus透视!w... wepokerplus透视!wepoker私人局透视教程(透视)插件-好像解迷有挂1、下载好wepo...