1. 若这个参数是个字符串,里面含有特殊字符
如: '我是一个字符&串' ,此时需要进行编码解码处理
1. 传参时编码
/pages/page1?string=" + encodeURIComponent(String);
2. 获取参数时解码
let value = decodeURIComponent(string);
2. 参数为对象,且里面含有特殊字符
1. 传参时先将对象转换为字符串之后再编码
/pages/page1?object=" + encodeURIComponent(JSON.stringify(object));
2. 接收参数解码后转成对象
let value = JSON.parse(decodeURIComponent(object));
参考:
- MDN官网encodeURIComponent: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
- decodeURIComponent: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
|