Angular之辅助路由

辅助路由

 即我们在某些场景路由加载组件时,还需要加载另外一些只负责显示的组件,此时我们可以使用辅助路由

定义组件

<a [routerLink]="[{outlets:{primary:'test2',aux1:'consult'}}]"   routerLinkActive="active">Test1</a>
<a [routerLink]="[{outlets:{aux:null}}]"   routerLinkActive="active">Test2</a>
<a [routerLink]="[ '/test1' ]"   routerLinkActive="active">Test1</a>
<router-outlet></router-outlet>
<router-outlet name="aux1"></router-outlet><!--定义一个aux1的辅助路由的出口 -->

当我们点击Test1时,<router-outlet></router-outlet>就会指向{outlets:{primary:'test2',aux1:'consult'}}中的primary对应的路由的组件

 <router-outlet name="aux1"></router-outlet>指向aux1:consult的路由

定义路由

const routes: Routes = [
  { path: '', redirectTo: '/test1', pathMatch: 'full' },
  { path: 'test1',component: Test1Component},
  { path: 'test2',component: Test2Component},
  { path: '**', component:Test1Component },
  { path: 'consult',component: Test2Component,outlet:"aux1"},

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

{ path: 'consult',component: Test2Component,outlet:"aux1"}就是我们定义的辅助路由。

实现效果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值