Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular2, while I have a named outlet open, how can I navigate to another route AND close the named outlet?

Tags:

angular

I'm using named outlets to show a sidebar. Works fine:

<a [routerLink]="['/', {outlets: {sidebar: 'profile'}}]">
    Open sidebar
</a>

I also have a link which closes the sidebar. Also works fine:

<a [routerLink]="['/', {outlets: {sidebar: null}}]">
    Close
</a>

Here's the problem. I want to have a third link, which links to a contacts route AND closes the sidebar. I tried doing this:

<a [routerLink]="['/contacts', {outlets: {sidebar: null}}]">
   Contacts
</a>

But nothing happens when I click that third link.

How can I navigate to another route and simultaneously remove a named outlet?

like image 222
Weblurk Avatar asked Nov 22 '16 15:11

Weblurk


People also ask

How do you use a named router-outlet?

Named outlet can be more than one per template. The component which needs to open in named outlet will configure its path with target outlet name in routing module using outlet property of Route interface. More than one named outlet can stay open together. Named outlet uses secondary routes to open a component.

Can we have more than one router-outlet in Angular?

Descriptionlink Using named outlets and secondary routes, you can target multiple outlets in the same RouterLink directive.

Can we use multiple router-outlet in Angular 2?

Yes you can as said by @tomer above. i want to add some point to @tomer answer. firstly you need to provide name to the router-outlet where you want to load the second routing view in your view. (aux routing angular2.)

What are the two elements of routes in Angular?

The first property, path , defines the URL path for the route. The second property, component , defines the component Angular should use for the corresponding path.


1 Answers

It turns out, when closing a named outlet like this, you also have to specify what the primary outlet should be.

So the third link should look like this:

<a [routerLink]="['/', {outlets: {sidebar: null}, {primary: 'contacts'}}]">
   Contacts
</a>
like image 149
Weblurk Avatar answered Oct 05 '22 14:10

Weblurk