Modifying the default URL encoding for Angular's router navigate method -
i need customize how urls encoded when calling navigate
method on router.
i know need implement own version of urlserializer
, write custom serialize
method. problem method wish change encode
method in url_tree
.
export function encode(s) { return encodeuricomponent(s); }
https://github.com/angular/angular/blob/master/modules/@angular/router/src/url_tree.ts#l344
in order need copy + paste following methods url_tree
customserializer
because not exported:
serialize
serializequeryparams
pair
pairs
serializesegment
encode
i've looked @ answer doesn't quite solve problem want default behaviour except encode
method.
am missing here or there better way solve issue?
angular2 default uses encodeuricomponent() encode queryparams in url, can avoid writing custom url serializer , override default functionality.
in case, wanted avoid angular2 avoid replacing comma(,) (%2c). passing query lang=en-us,en-uk getting converted lang=en-us%2cen-uk.
here how worked out:
customurlserializer.ts
import {urlserializer, urltree, defaulturlserializer} '@angular/router'; export class customurlserializer implements urlserializer { parse(url: any): urltree { let dus = new defaulturlserializer(); return dus.parse(url); } serialize(tree: urltree): { let dus = new defaulturlserializer(), path = dus.serialize(tree); // use regex replace per requirement. return path.replace(/%2c/g,','); } }
add below line main appmodule.ts
import {urlserializer} '@angular/router'; import {customurlserializer} './customurlserializer'; @ngmodule({ providers: [{ provide: urlserializer, useclass: customurlserializer }] })
this won't break default functionality , take cares of url per need.
Comments
Post a Comment