EncodeURIComponent and decodeURIComponent

NavigateTo is truncated because the data is too long. In fact, the data length of wechat parameter can reach about 45KB, which can almost transfer all the data in the work.

There are special characters in the link

Like this link: mp.weixin.qq.com/s?__biz=MzA…

Normal submissions are truncated because of the special character question mark? .

The solution

1, using encodeURIComponent splicing parameters after coding, splicing on the link, in the onLoad life cycle of the accepted page, after receiving the parameters, using decodeURIComponent decoding can be used normally.

let test = https://mp.weixin.qq.com/s?__biz=MzA4MTQ2NDg1NQ==&mid=504984964&idx=1&sn=b9fc08b796fec296ec5e25feb6cf6c&chksm=04794efc33 0ec7ea6dbd40d9e16326e24d05a30e55eaad1af1c96e2d6514658cae5366d2dbd#rd wx.navigateTo({ url: ', / pages/index/index? Obj = encodeURIComponent (test) '}) / / decoding onLoad: function (option) {let {obj} = option | | {}; obj = decodeURIComponen(obj); // Then use normal}Copy the code

Second, the eventChannel

1. Use wechat official communication method, eventChannel, to communicate with the opened page.

Take a look at the official link below to understand:

  • The current page
wx.navigateTo({ url: 'test? Id = 1 ', events: {/ / add a listener to a specified event, access to open the page data transferred to the current page acceptDataFromOpenedPage: function(data) { console.log(data) }, someEvent: function(data) { console.log(data) } ... }, success: function(res) {emit('acceptDataFromOpenerPage', {data: 'test' }) } })Copy the code
  • Receive page
//test.js Page({ onLoad: function(option){ console.log(option.query) const eventChannel = this.getOpenerEventChannel() eventChannel.emit('acceptDataFromOpenedPage', {data: 'test'}); eventChannel.emit('someEvent', {data: 'test'}); // Listen for acceptDataFromOpenerPage events, Eventchannel. on('acceptDataFromOpenerPage', function(data) {console.log(data)})}})Copy the code

Official link stamp here!

Conclusion:

It is not recommended to pass too many parameters. If there are too many data parameters, try to use the ID and query the parameters on the next page.