Angular路由默认辅助路由
创始人
2024-10-28 02:32:36
0

在Angular中,可以通过在路由配置中使用outlet属性来定义辅助路由。默认情况下,Angular会使用primary作为主要路由的outlet名称。

下面是一个示例,演示如何在Angular中添加默认辅助路由:

首先,在路由模块中定义辅助路由的组件和路径:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { MainComponent } from './main.component';
import { SidebarComponent } from './sidebar.component';
import { DashboardComponent } from './dashboard.component';

const routes: Routes = [
  {
    path: '',
    component: MainComponent,
    children: [
      { path: '', component: DashboardComponent },
      { path: 'sidebar', component: SidebarComponent, outlet: 'sidebar' }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

在上面的示例中,我们定义了一个名为sidebar的辅助路由,并将其组件SidebarComponent与之关联。

接下来,在主组件的模板中,使用router-outlet指令来显示主要路由和辅助路由:


在上面的示例中,我们使用router-outlet指令两次,分别用于显示主要路由和辅助路由。

最后,在主组件的控制器中,可以使用Router来导航到辅助路由:

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.css']
})
export class MainComponent {

  constructor(private router: Router) { }

  showSidebar() {
    this.router.navigate([{ outlets: { sidebar: 'sidebar' } }]);
  }

}

在上面的示例中,我们使用Routernavigate方法,并在outlets参数中指定sidebar辅助路由的路径。

现在,当调用showSidebar方法时,辅助路由将显示在名为sidebarrouter-outlet中。

请注意,当导航到辅助路由时,主要路由仍然保持不变。

相关内容

热门资讯

透视游戏!pokemmo脚本手... 透视游戏!pokemmo脚本手机版,黑侠破解wepoker(透视)果然真的有挂(必赢方法);1)po...
透视脚本!xpoker辅助控制... 透视脚本!xpoker辅助控制,pokerworld破解版下载,原来是有挂(德州论坛)1、构建自己的...
透视智能ai!智星德州插件怎么... 透视智能ai!智星德州插件怎么下载,智星菠萝有挂吗(透视)好像是真的有挂(必胜教程)1、玩家可以在智...
透视辅助!德州透视脚本,wer... 透视辅助!德州透视脚本,werplan外卦神器,其实是真的有挂(必备教程)1、下载好werplan外...
透视能赢!wepoker私人定... 透视能赢!wepoker私人定制透视,wpk免费辅助(透视)素来存在有挂(解密教程)1、游戏颠覆性的...
透视透视挂!拱趴大菠萝有挂吗,... 透视透视挂!拱趴大菠萝有挂吗,uupoker透视,果然是真的有挂(我来教教你);拱趴大菠萝有挂吗辅助...
透视私人局!德扑HHpoker... 透视私人局!德扑HHpoker有挂吗,pokemmo手机版透视脚本(透视)都是有挂(安装教程);1、...
透视新版!聚星ai辅助工具激活... 透视新版!聚星ai辅助工具激活码,德州局怎么透视,一直有挂(玩家教你)1、玩家可以在聚星ai辅助工具...
透视玄学!wepoker私人局... 透视玄学!wepoker私人局外卦,wepoker辅助真的假的(透视)一贯是真的有挂(普及教程)1、...
透视肯定!pokemmo手机脚... 透视肯定!pokemmo手机脚本,pokemmo脚本辅助下载,竟然真的是有挂(必备教程)1、poke...