為什么要使用encodeuricomponent?
1、encodeuricomponent
可把字符串作為 URI 組件進行編碼。
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ’ ( ) 。
其他字符(比如 :;/?😡&=+$,# 這些用於分隔 URI 組件的標點符號),都是由一個或多個十六進制的轉義序列替換的。
2、encodeuricomponent什么時候使用:用於url作為參數傳遞的場景中使用
url當作參數傳遞的時候,當參數出現空格這樣的特殊字段,后台只可以讀取到空格前的內容,后面內容丟失,造成數據讀取失敗,但是如果用encodeURIComponent(),則這些特殊字符進行轉義,這樣后台就可以成功讀取了,所以encodeURIComponent()用於url作為參數傳遞的場景中使用。
2、decodeURIComponent
decodeURIComponent() 函數可對 encodeURIComponent() 函數編碼的 URI 進行解碼。
例如:
傳參數頁面
var res = encodeURIComponent(JSON.stringify(res))
console.log(res)
wx.navigateTo({ url: '/pages/addaddress/main?res='+res });
接收數據的頁面:
mounted () {
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;
}
},