Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do aux routes work for the root component only?

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:

Blue: Primary Outlet, Red: Aux Outlet

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:

Test3 is rendered inside Shell

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.

like image 687
Thorsten Westheider Avatar asked Jul 06 '16 12:07

Thorsten Westheider


People also ask

What are auxiliary routes in Angular?

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.

Can you have multiple router outlets in Angular?

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.

Can Angular 8 use multiple router outlets?

Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes.

What does router-outlet do in Angular?

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.


2 Answers

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

like image 64
Arpit Agarwal Avatar answered Nov 15 '22 04:11

Arpit Agarwal


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>
  `
})
like image 27
Francis Manoj Fernnado Avatar answered Nov 15 '22 05:11

Francis Manoj Fernnado