同时更改两个独立的路由-可以使用辅助路由吗?

时间:2020-04-07 17:28:55

标签: angular routing angular9

通过辅助路由,我们可以在同一组件中进行两个独立的路由。通过对单个URL使用相同的概念,我们可以同时更改独立的路由吗?

例如:

  1. http://localhost:4200/listing对于此路由,我想针对名称路由出口加载C1组件并从主路由出口列出组件。
  2. http://localhost:4200/listings,对于此路由,我想针对名称路由出口加载C2组件,并从主路由出口加载列表组件。

    const appRoutes: Routes = [
        { path: '', component: HeroComponent, outlet: 'hero', children: [
        { path: 'listings', component: C2Component},
        { path: 'listing', component: C1Component},]},
    
        { path: '', component: ContentComponent, children: [
        { path: '', component: HomeComponent},
        { path: 'listings', component: ListingsComponent},
        { path: 'listing', component: ListingComponent},
        { path: 'sellers', component: SellersComponent},
        { path: 'seller', component: SellerComponent}]}];
    

1 个答案:

答案 0 :(得分:0)

尝试一下

    { path: 'listing', component: C1Component, outlet:'hero'},
    { path: 'listing', component: ListingComponent},
    { path: 'listings', component: C2Component, outlet:'hero'},
    { path: 'listings', component: ListingsComponent},

根据路由,同一路由具有两个配置,因此名称为hero的插座将获得C1Component,而主路由器出口将获得列表组件

const appRoutes: Routes = [
    { path: '', component: HeroComponent, outlet: 'hero'},

    { path: 'listing', component: C1Component, outlet:'hero'},
    { path: 'listing', component: ListingComponent},
    { path: 'listings', component: C2Component, outlet:'hero'},
    { path: 'listings', component: ListingsComponent},

    { path: '', component: ContentComponent, children: [
    { path: '', component: HomeComponent} 
    { path: 'sellers', component: SellersComponent},
    { path: 'seller', component: SellerComponent}]
}];