浏览器地址栏路径中有百分号%在history包中进行decode报错

场景:react-router-dom5:"^5.2.0",依赖的history在创建location时,createLocation方法中调用decodeURI,

location.pathname = decodeURI(location.pathname);

location.pathname初次是%25,解码后变%,后来又有其他地方点用createLocation此时decodeURI('%')报错

也就是说decodeURI这个方法总是把%25解码,其他符号不会被解码,导致报错

 

解决方式:

location.pathname.replace(new RegExp('%25', 'g'), '%2525');

 

小常识:

decodeURI('/search/result/%25')
"/search/result/%"
decodeURI('/search/result/%23')
"/search/result/%23"

 

posted @ 2021-06-17 17:54  二双  阅读(502)  评论(0编辑  收藏  举报