Auxiliary routes in Angular 2 remains in the url after navigating away

纵饮孤独 提交于 2019-12-12 04:28:41

问题


I'm trying to use auxiliary routes to show different content in a sidebar, depending on which route you're on.

The problem is that the auxiliary route remains (both in URL and the content) after navigating away.

The HTML:

<router-outlet></router-outlet>
<router-outlet name="sidebar"></router-outlet>

The RouteConfig:

@RouteConfig([
    {path: '/', name: 'Home', component: Home, useAsDefault: true},
    {path: '/leadslist', name: 'LeadsList', component: LeadsList},
    {aux:'/sidebar', name: 'Sidebar', component: Sidebar}
])

The links in the navbar:

<a [routerLink]="['Home']">Home</a>    
<a [routerLink]="['LeadsList', ['Sidebar']]">Leads list</a>

Current Behaviour:

  1. From the home page, I click the link "Leads list".
  2. I am redirected to the route "/leadslist(sidebar)" and the sidebar content is properly loaded in its router outlet. All good so far.
  3. If I now click the link "Home" to take me back to "/", the URL is instead "/(sidebar)", which takes me back to the home page but with the sidebar loaded (unwanted behaviour!)

My question: How can I navigate to a route without the auxiliary route "tagging along"?


回答1:


Currently (imho) this could be handled better in the Angular2 router, but what you need to do is set your aux outlet to null and point the primary outlet with the url you want to go back to.

This works because primary is a reserved default outlet for angular2.

Consider the following route: /inbox/123131$asd(compose:reply)

In the above route we were viewing an email and composing a reply. So - when we send this we might want the user to get sent back to the main view - the inbox. Then your routerLink would need to look like the following:

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

Be wary that this might be subject to change in the third rewrite of the Angular2 router.



来源:https://stackoverflow.com/questions/36325376/auxiliary-routes-in-angular-2-remains-in-the-url-after-navigating-away

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!