解决微信小程序传参字符过长的问题

1: 使用 wx.navigateTo 进行页面跳转传参

传入:
wx.navigateTo({
  url:'/page/testPage/testPage/',
  success: function (res) {
    res.eventChannel.emit('getParamsData', pageParams)  //触发事件 pageParams 为传递参数
  }
})

 

接收:(接收页面)
onLoad: function(options) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on("getParamsData",data => { 
      console.log(data) 
     })
}

 

2: 使用   encodeURIComponent  decodeURIComponent 对传参进行转码

 

传入:
wx.redirectTo({
   url: `/pages/testPage/testPage?params=${encodeURIComponent(JSON.stringify(testParams))`
})

 

接收:(接收页面)
 onLoad: function (options) { 
  const param = JSON.parse(decodeURIComponent(options.testParams)) 
 }

 

posted @ 2023-03-25 17:14  收破烂的小伙子  阅读(27)  评论(0编辑  收藏  举报