Angular12路由在URL中显示UTF-8字符,如“%2F”、“%3F”,导致无法匹配任何路由。URLSegment错误。
创始人
2024-10-21 20:01:18
0

这个问题通常出现在路由导航时,当 URL 中包含特殊字符时。解决方法是使用 Angular 的 UrlEncodePipe 对 URL 进行编码。

在 HTML 模板中,将路由链接写为:

...

在路由定义中,使用 pathMatch: 'full' 来确保完全匹配:

{ path: 'my-route/:id', component: MyRouteComponent, pathMatch: 'full' },

最后,在应用的 AppModule 中提供 UrlEncodePipe:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { UrlEncodePipe } from './url-encode.pipe';

@NgModule({
  imports: [BrowserModule],
  declarations: [UrlEncodePipe],
  providers: [
    { provide: RouteReuseStrategy, useClass: CustomRouteReuseStrategy },
    { provide: UrlEncodePipe, useClass: UrlEncodePipe },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

使用 UrlEncodePipe 对 URL 进行编码后,特殊字符会被转换成它们的 UTF-8 编码,并且可以匹配路由。

相关内容

热门资讯

透视经验!德普之星透视辅助软件... 透视经验!德普之星透视辅助软件激活码(透视)总是真的是有辅助安装(哔哩哔哩)1、每一步都需要思考,不...
透视积累!wpk系统是否存在作... 透视积累!wpk系统是否存在作必弊行为(透视)好像存在有辅助安装(哔哩哔哩)1)wpk系统是否存在作...
透视资料!安装不了wepoke... 透视资料!安装不了wepoker(透视)本来是有辅助修改器(哔哩哔哩)1、安装不了wepoker辅助...
透视经验!wepoker透视脚... 透视经验!wepoker透视脚本下载(透视)其实是有辅助插件(哔哩哔哩)进入游戏-大厅左侧-新手福利...
透视指引!wepoker透视底... 透视指引!wepoker透视底牌脚本(透视)确实存在有辅助软件(哔哩哔哩)1、这是跨平台的wepok...
透视资料!wepoker辅助工... 透视资料!wepoker辅助工具(透视)切实真的是有辅助app(哔哩哔哩)一、wepoker辅助工具...
透视要领!hhpoker怎么防... 透视要领!hhpoker怎么防作必弊(透视)一直是真的辅助app(哔哩哔哩)1、hhpoker怎么防...
透视练习!wpk辅助购买(透视... 透视练习!wpk辅助购买(透视)其实存在有辅助辅助器(哔哩哔哩)1、wpk辅助购买有没有辅助教程、w...
透视窍门!wepoker破解器... 透视窍门!wepoker破解器激活码(透视)真是存在有辅助插件(哔哩哔哩)1、上手简单,内置详细流程...
透视模板!云扑克有透视吗(透视... 透视模板!云扑克有透视吗(透视)一贯真的是有辅助下载(哔哩哔哩)1、透视模板!云扑克有透视吗(透视)...