小程序传参接收报错

175 阅读1分钟

引发原因

由于小程序调整没有设置默认值导致undefined变成了字符串导致页面跳转报错,小程序报错提示也不够清晰

onLoad: async function (options) {
 console.log(options?.redirectPath,typeof(options?.redirectPath),this.data.redirectPath,typeof(this.data.redirectPath),'this.data.redirectPath');
 // 传参前未设置redirectPath时 'undefined' string  '' string 导致后面跳转报错
 
 // 传参有设置redirectPath时 '' string  '' string
 wx.redirectTo({
      url: `${decodeURIComponent(this.data.redirectPath)}`
  })
}

解决方案

wx.navigateTo({
            url: `/pages/login/login?role=${role}&redirectPath=${redirectPath||''}`,
})

image.png