angular2 routing - Custom encoding for urls using Angular 2 Router (using a + sign in place of a space) -


i using angular 2 router update query params in url search application. attempting replace spaces in query + signs. however, + signs getting encoded. example:

this.router.navigatebyurl('?q=one+two'); 

populates url "?q=one%2btwo".

in looking @ source code angular 2, looks router converts url urltree which uses encodeuricomponent() encode url. because of this, impossible prevent default encoding.

my current process change route doing navigatebyurl seen above, , listen changes with:

this.routesubscription = this.route.queryparams.subscribe((params: any) => {   this.term = (params.q ? params.q : ''); }); 

is there alternate way deal query parameters allow me use own strategy url encoding?

i able find solution problem. can make own custom url serializer implementing urlserializer class.

custom url serializer

create custom url serializer this:

class customurlserializer implements urlserializer {     parse(url: string): urltree {         // custom code here     }      serialize(tree: urltree): string {         // custom code here     } } 

then, need provide customurlserializer in place of urlserializer, can place in appmodule providers array after importing both serializers.

providers: [     { provide: urlserializer, useclass: customurlserializer },     ... ] 

now, when call router.navigate or router.navigatebyurl, use custom serializer parsing , serializing.

using + signs spaces

to parse + signs spaces:

parse(url: string): urltree {     // change plus signs encoded spaces     url = url.replace(/\+/g, '%20');     // use default serializer can import      // default parsing have fixed url.     return this.defaulturlserializer.parse(url)   } 

and serializing:

serialize(tree: urltree): string {     // use default serializer create url , replace spaces + signs     return this.defaultserializer.serialize(tree).replace(/%20/g, '+'); } 

defaulturlserializer


Comments

Post a Comment

Popular posts from this blog

php - isset function not working properly -

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -