javascript – angular 2禁用网址编码
作者:互联网
我想禁用网址编码.
当我在下面使用这个.
this.router.navigate(['/profile', { tags: 'one,two' }]);
网址是这样的
http://localhost:4200/profile;tags=one%2Ctwo
我希望它非常像下面
http://localhost:4200/profile;tags=one,two
有没有办法禁用网址编码?
解决方法:
Angular2默认使用encodeURIComponent()来编码URL中的queryParams,您可以通过编写自定义URL序列化程序并覆盖默认功能来避免它.
在我的情况下,我想避免Angular2避免用(%2)替换逗号(,).我传递了Query作为lang = en-us,en-uk,它被转换为lang = en-us.n-uk.
在这里我是如何解决的:
CustomUrlSerializer.ts
import {UrlSerializer, UrlTree, DefaultUrlSerializer} from '@angular/router';
export class CustomUrlSerializer implements UrlSerializer {
parse(url: any): UrlTree {
let dus = new DefaultUrlSerializer();
return dus.parse(url);
}
serialize(tree: UrlTree): any {
let dus = new DefaultUrlSerializer(),
path = dus.serialize(tree);
// use your regex to replace as per your requirement.
return path.replace(/%2/g,',');
}
}
将以下行添加到主appModule.ts
import {UrlSerializer} from '@angular/router';
import {CustomUrlSerializer} from './CustomUrlSerializer';
@NgModule({
providers: [{ provide: UrlSerializer, useClass: CustomUrlSerializer }]
})
这不会破坏您的默认功能,并根据您的需要处理URL.
标签:javascript,angular,http,angular2-routing 来源: https://codeday.me/bug/20190917/1808776.html