小程序页面跳转传参被截断(丢失参数)

我们在开发小程序的过程中,有时候会在小程序页面跳转的时候传参,url会被截断。

//我需要传递的参数
let videoDetail = "https://xxxxxxxxxx/index.html?imid=2019112116065477552274061212192768#/Index?imid=2019112116065477552274061212192768&apitoken=81df16bb-e68d-4efa-a4ad-07ad9eeb8de6WXorqMct5MXR20vWYiFTk_n2wTu8Qw1212192768"
//页面跳转
wx.navigateTo({
    url: '/pages/homePage/mall/webView/webView?imUrl=' + videoDetail
})

解决办法:需要对参数进行编码(下边是传入的方法)

var videoDetail = encodeURLComponent(str + "&apitoken=" + res.data);
console.log(videoDetail)
//页面跳转
wx.navigateTo({
	url: "/pages/homePage/mall/webView/webView?imUrl=" + videoDetail
})

(接收的方法)

onLoad: function (options) {
	console.log(options);
	let url = decodeURLComponent(options.imUrl);
	console.log(url)
}

encodeURLComponent()

返回编码为统一资源标识符 (URI) 的有效组件的字符串。

备注:encodeURIComponent 方法返回一个已编码的 URI。如果将编码结果传递给 decodeURIComponent,则将返回初始的字符串。因为 encodeURIComponent 方法编码所有字符,所以请注意该字符串是否表示路径,例如 /folder1/folder2/default.html。斜杠字符将被编码,因此如果作为请求发送到 Web 服务器将无效。如果字符串中包含多个 URI 组件,请使用 encodeURI 方法进行编码。

decodeURLComponent()

返回统一资源标识符 (URI) 的一个已编码组件的非编码形式。

备注:URIComponent 是一个完整的 URI 的一部分。如果 encodedURIString 无效,则将产生 URIError。

你可能感兴趣的:(微信小程序,小程序,前端)