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中。

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

相关内容

热门资讯

最终!we poker游戏下(... 最终!we poker游戏下(透视)聚友联盟免费辅助器(好像真的是有辅助app)-哔哩哔哩1、该软件...
备受关注的!wepokerpl... 备受关注的!wepokerplus开挂(透视)浙江宝宝游戏透视辅助器(原来是真的辅助工具)-哔哩哔哩...
事发当天!德州局怎么透视(透视... 事发当天!德州局怎么透视(透视)透视辅助器免费蜀山四川(竟然真的有辅助神器)-哔哩哔哩透视辅助器免费...
有玩家发现!wepoker私人... 有玩家发现!wepoker私人局俱乐部怎么进(透视)火神大厅辅助器(切实真的有辅助平台)-哔哩哔哩1...
透视黑科技!wepoker脚本... 透视黑科技!wepoker脚本下载(透视)花城棋舍插件安装(其实是真的辅助辅助器)-哔哩哔哩1、起透...
透视脚本!德普之星辅助器(透视... 透视脚本!德普之星辅助器(透视)乐逍遥脚本辅助(其实有辅助神器)-哔哩哔哩1、实时乐逍遥脚本辅助透视...
透视软件!wepoker安装教... 透视软件!wepoker安装教程(透视)福建开心辅助(果然真的是有辅助app)-哔哩哔哩1、实时福建...
透视辅助!wepoker钻石怎... 透视辅助!wepoker钻石怎么看底牌(透视)潮汕来物局透视平台使用方法(总是有辅助app)-哔哩哔...
据统计!wpk软件是正规的吗(... 据统计!wpk软件是正规的吗(透视)拱趴大菠萝调胜率(确实真的有辅助工具)-哔哩哔哩一、拱趴大菠萝调...
透视苹果版!德普之星透视免费(... 透视苹果版!德普之星透视免费(透视)微信中至有辅助吗(总是存在有辅助app)-哔哩哔哩1、用户打开应...