Angular <router-outlet name=“popup”> change URL path structure?

拥有回忆 提交于 2019-12-17 17:03:54

问题


I'm using a popup (refering to the docs): https://angular.io/guide/router#displaying-multiple-routes-in-named-outlets

Everything is fine apart from the URL structure:

/domain/subPath/(popup:myopoup)

How can I change this default structure with parenthesis? I would like it to be as follows:

/domain/subPath/popup

In other words, I want to remove the brackets including the colon inside the URL.

Inside their documentation the popup also appears in that manner (with brackets)

Here is some code:

.ts

{
  path: 'mypopup',
  component: MyComponent,
  outlet: 'popup'
},

.html

<a [routerLink]="[{ outlets: { popup: ['mypopup'] } }]">Contact</a>
<router-outlet name="popup"></router-outlet>

回答1:


you can use URL serializer, to change url structure

import { UrlTree ,DefaultUrlSerializer, UrlSerializer } from '@angular/router';

export class cleanUrlSerializer extends DefaultUrlSerializer {  
public parse(url: string): UrlTree {
    function cleanUrl(url) {
        return url.replace(/\(|\)/g,'') // for example to delete parenthesis
    }
    return super.parse(cleanUrl(url));
}
}

import this class and add it as provider in your module

 providers: [
    {
        provide: UrlSerializer,
        useClass: cleanUrlSerializer 
    }
  ]



回答2:


That is how Angular handles multiple outlets. I do not think you will be finding a "clean" solution for this. Just dont use name router outlets, then. Have one single router outlet without a name.

[routerLink]="['popup']">

{
  path: 'mypopup',
  component: MyComponent
},

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

Unfortunately, it seems like you do have multiple routers in your project, so this is probably not a solution. I will backup the other commenters when they said there currently is not an easy clean way. I also want a different routing solution, so hopefully we can complain enough that they find a new way.



来源:https://stackoverflow.com/questions/49515140/angular-router-outlet-name-popup-change-url-path-structure

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