为什么要使用encodeuricomponent?

为什么要使用encodeuricomponent?

1、encodeuricomponent

可把字符串作为 URI 组件进行编码。

该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ’ ( ) 。

其他字符(比如 :;/?😡&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

2、encodeuricomponent什么时候使用:用于url作为参数传递的场景中使用

url当作参数传递的时候,当参数出现空格这样的特殊字段,后台只可以读取到空格前的内容,后面内容丢失,造成数据读取失败,但是如果用encodeURIComponent(),则这些特殊字符进行转义,这样后台就可以成功读取了,所以encodeURIComponent()用于url作为参数传递的场景中使用。

3、decodeURIComponent

decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。

例如:

传参数页面

console.log(res)
wx.navigateTo({ url: '/pages/addaddress/main?res='+res });

接收数据的页面:

    this.openId = wx.getStorageSync('openId')||'';
    //看是从哪个页面跳转过来(获取跳转过来的信息)
    if(this.$root.$mp.query.res){
      this.res = JSON.parse(decodeURIComponent(this.$root.$mp.query.res))
      console.log(this.res)
      console.log(typeof this.res)
      this.userName = this.res.userName;
      this.telNumber = this.res.telNumber;
      this.address = `${this.res.provinceName} ${this.res.cityName} ${this.res.countyName}`;
      this.detailadress = this.res.detailInfo;
    }
},
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容