我将提供程序自定义 UrlSerializer 添加到根 AppModule。
像这样...
// custom-url-serializer.ts
import { DefaultUrlSerializer, UrlSerializer, UrlTree } from '@angular/router';
export class CustomUrlSerializer implements UrlSerializer {
private defaultUrlSerializer: DefaultUrlSerializer = new DefaultUrlSerializer();
parse(url: string): UrlTree {
url = url.replace(/\%/g, '%25');
return this.defaultUrlSerializer.parse(url);
}
serialize(tree: UrlTree): string {
return this.defaultUrlSerializer.serialize(tree).replace(/%25/g, '%');
}
}
// app.module.ts
import { NgModule } from '@angular/core';
import { CustomUrlSerializer } from './shared/custom-url-serializer';
@NgModule({
declarations: [
AppComponent,
],
imports: [
...
],
bootstrap: [AppComponent],
providers: [
{
provide: UrlSerializer,
useClass: CustomUrlSerializer, // <-- Here!
},
],
})
export class AppModule { }