微信小程序之页面跳转传递参数为数组对象以及字符串长度过长问题
2019-07-19 16:35
766 查看
最近在搞小程序,发现页面跳转传递的参数不能为数组对象,如何能让页面跳转时传递数组对象呢
[code][0: {did: "1", dtitle: "数据结构的基本概念-1", durl: "https://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/no…62cd6_sd.mp4?ak=7909bff134372bffca53cdc2c17adc27a", video: null} 1: {did: "2", dtitle: "数据结构的基本概念-2", durl: "https://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/no…3905d_sd.mp4?ak=7909bff134372bffca53cdc2c17adc27a", video: null} 2: {did: "3", dtitle: "数据结构的基本概念-3", durl: "https://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/no…2ec77_sd.mp4?ak=7909bff134372bffca53cdc2c17adc27a", video: null} 3: {did: "4", dtitle: "数据的逻辑结构和存储结构", durl: "https://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/no…8fed7_sd.mp4?ak=7909bff134372bffca53cdc2c17adc27a", video: null} 4: {did: "5", dtitle: "算法及其事件复杂度", durl: "https://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/no…eca7a_sd.mp4?ak=7909bff134372bffca53cdc2c17adc27a", video: null} 5: {did: "6", dtitle: "时间复杂度及其应用", durl: "https://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/no…2d712_sd.mp4?ak=7909bff134372bffca53cdc2c17adc27a", video: null}]
这是我要向另一个页面传递的数据
首先将json类型的数据转换为字符串,利用JSON.stringify()
[code]vedioItemToDetail(event){ console.log(event); var videoDetail =JSON.stringify(event.currentTarget.dataset.item.videoDetail); console.log(videoDetail) wx.navigateTo({ url: '/pages/detail/detail?videoDetail=' + videoDetail, }) },
跳转页面后将字符串转换为json数据,利用JSON.parse()
[code]var videoDetailList = JSON.parse(options.videoDetail);
But,页面传递的字符串长度是有限制的,过多的字符串是会被截取的,这就非常的难受了
然而办法还是有滴,请看:
[code]vedioItemToDetail(event){ console.log(event); var videoDetail =encodeURIComponent(JSON.stringify(event.currentTarget.dataset.item.videoDetail)); console.log(videoDetail) wx.navigateTo({ url: '/pages/detail/detail?videoDetail=' + videoDetail, }) },
[code]var videoDetailList = JSON.parse(decodeURIComponent(options.videoDetail));
完美解决数据被截取的问题
tips:
JSON.stringify():JSON.stringify() 方法将 JavaScript 对象转换为字符串。
JSON.parse():用于将一个 JSON 字符串转换为对象。
encodeURIComponent():可把字符串作为 URI 组件进行编码。
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。
decodeURIComponent():可对 encodeURIComponent() 函数编码的 URI 进行解码。
相关文章推荐
- 小程序之页面跳转传递对象参数问题
- 微信小程序传递参数(字符串、数组、对象)
- 微信小程序——页面跳转传递参数(实体,对象)
- 微信小程序传递参数(字符串、数组、对象)
- 微信小程序页面跳转传递Json对象参数
- [微信小程序]页面跳转对象参数的传递方法
- [转] 微信小程序 页面跳转 传递参数
- C程序中,strlen是求取字符串长度,若对整形数组,求取的是什么?还有整形数组作函数参数的问题?
- 微信小程序的页面跳转和参数传递 —— 微信小程序教程系列(6)
- 微信小程序中页面跳转时如何传递一个对象
- 微信小程序之页面跳转和参数传递
- 微信小程序 页面跳转navigator与传递参数
- 微信小程序 页面跳转 传递参数
- 微信小程序详解——页面之间的跳转方式【路由】和参数传递
- 微信小程序之页面跳转传参数据:json 字符串转对象报错
- 微信小程序的页面跳转和参数传递
- 微信小程序学习笔记之跳转页面、传递参数获得数据操作图文详解
- 微信小程序 页面跳转 传递参数
- 微信小程序详解——页面之间的跳转方式【路由】和参数传递
- 微信小程序 页面跳转 传递参数