I'm having trouble setting up auxiliary routes in child components, for some reason only those auxiliary routes work that start at the root component.
Here's my router setup
export const routes: RouterConfig = [
{ path: 'test1', component: Test1Component },
{ path: 'test2', component: Test2Component, outlet: 'aux'},
{ path: 'shell', component: ShellComponent, children: [
{ path: 'department/:id', component: DepartmentDetailComponent },
{ path: 'test3', component: Test3Component, outlet: 'aux2' } ] }
];
If I navigate to
http://localhost:3000/shell/department/1(aux:test2)
then the output is as expected, that is, Test2Component
is rendered inside AppComponent
, along with ShellComponent
and DepartmentDetailComponent
:
Primary outlets show up in blue, auxiliary outlets in red.
If, however, I try to navigate to
http://localhost:3000/shell/department/1(aux2:test3)
I get an error message:
platform-browser.umd.js:1900 EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: 'test3'
router-outlets
are as follows:
app.component.ts (aux: test2)
<div class="app">
<h1>App</h1>
<div class="primary-outlet">
<router-outlet></router-outlet>
</div>
<div class="aux-outlet">
<router-outlet name="aux"></router-outlet>
</div>
</div>
shell.component.ts (aux2: test3)
<div class="component">
<h1>Shell</h1>
<div class="primary-outlet">
<router-outlet></router-outlet>
</div>
<div class="aux-outlet">
<router-outlet name="aux2"></router-outlet>
</div>
</div>
What am I missing?
EDIT: As suggested by Arpit Agarwal, navigating to
http://localhost:3000/shell/(department/1(aux2:test3))
does the trick:
However, take a look at the URL after page load. If I press F5
now, I'm back to square one:
platform-browser.umd.js:1900 EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: 'shell'
EDIT 2: Here's the link to the project on github.
Auxiliary Routes are independent, secondary routes that can be added to a page along with a primary route. As an example, we can have a side-menu that lives within the primary route with its own router-outlet : that's an auxiliary route.
You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc.
Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes.
Router-Outlet is an Angular directive from the router library that is used to insert the component matched by routes to be displayed on the screen.
Try using http://localhost:3000/shell/(department/1//aux2:test3)
URL has format (primaryroute//secondaryroute)
parentheses tells it may have sibling routes and //
is sibling route separator.
Aux and primary outlets are considered sibling on same parent
some working example click here
important points
<a [routerLink]="['/component-one',{ outlets: { 'sidebar': ['component-aux'] } }]">Component One</a>
@Component({
selector: 'component-one',
template: `Component One
<div style="color: green; margin-top: 1rem;">Sidebar Outlet:</div>
<div style="border: 2px solid blue; padding: 1rem;">
<router-outlet name="sidebar"></router-outlet>
</div>
`
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With