Angular Route Default Auxiliary Routes

Viewed 381

When I read about Angular Router's auxiliary routing it is discussed as being an "independent" set of routes. However, what I'm trying to create "dependent" routes. For example:

/home -> HomeComponent, WelcomeComponent
/about -> AboutComponent, SearchComponent
/products -> ProductsComponent, ProductListComponent
/pricing -> PricingComponent, WelcomeComponent

I want to be able to just specify the main (page) route like the following code and based on my example above load these two components (AboutComponent, SearchComponent).

<a routerLink="/about">About</a>

I can get the two components to load when I use the following:

<a [routerLink]="[{ outlets: { primary: ['about'], sidebar: ['search'] } }]">
    Products List
</a>

However, I really don't want the link to know about the auxiliary route. I want it supplied automatically. Is this possible with the right configuration? Or will this require me to extend the RouteModule to do this?

Edit:

Another requirement I have to is to keep the peer to each other. The layout of the site is such that the second component is not within the main component.

2 Answers

You have 2 options based on my knowledge.

Option 1 : create a main route with a children component

app.routing.ts

const routes: Routes = [
  {
   path: 'home', component: HomeComponent,
   children: [{ path: '', component: WelcomeComponent }],
  },
  {
   path: 'about', component: AboutComponent,
   children: [{ path: '', component: SearchComponent }],
  },
  {
   path: 'products ', component: ProductsComponent,
   children: [{ path: '', component: ProductListComponent }],
  },
  {
   path: 'pricing  ', component: PricingComponent,
   children: [{ path: '', component: WelcomeComponent }],
  }
}];

You have your main component e.g. AboutComponent. And in it, you have to put a <router-outlet> to place your children component.

about.component.html

<!-- code of your component -->
<router-outlet></router-outlet>
<!-- code of your component -->

Option 2 :

you put your children component directly in your parent component but you cannot call that a routing at that point.

about.component.html

<!-- code of your component -->
<app-search-component></app-search-component>
<!-- code of your component -->
Related