Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 359
Eugen Sunic Avatar asked Mar 27 '18 14:03

Eugen Sunic


1 Answers

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 
    }
  ]
like image 58
Fateh Mohamed Avatar answered Nov 05 '22 20:11

Fateh Mohamed