微信小程序的页面数据传递

您所在的位置:网站首页 网页参数传递 微信小程序的页面数据传递

微信小程序的页面数据传递

2023-03-16 02:22| 来源: 网络整理| 查看: 265

页面的数据传递有两种方式:

通过url带参数的方式传递 基本数据类型的传递与接收

传递基本数据类型 Page({ data: { str: '字符串类型str' }, onLoad: function () { }, next: function(e){ wx.navigateTo({ url: '/pages/test/test?str='+this.data.str, }) } }) 接收基本数据类型(所有传递过来的参数都在 options中可以得到) Page({ data:{ }, onLoad:function(options){ console.log("接收到的参数是str="+options.str); } })

 

2、传递对象

传递对象,使用了json.stringify(Object)方法 Page({ data: { obj: {name:'我是name', extra:'我是extra'} }, onLoad: function () { }, next: function(e){ wx.navigateTo({ url: '/pages/test/test?extra='+JSON.stringify(this.data.obj) }) } }) 接收(所有传递过来的参数都在 options中可以得到) Page({ data:{ obj:null }, onLoad:function(options){ this.data.obj = JSON.parse(options.extra); //解析对象 } })

3、传递数组

传递数组,也使用了json.stringify(Object)方法 Page({ data: { arry: ["1","2","3"] }, onLoad: function () { }, next: function(e){ wx.navigateTo({ url: '/pages/test/test?arry='+JSON.stringify(this.data.arry) }) } }) 接收(所有传递过来的参数都在 options中可以得到) Page({ data:{ arry:[] } onLoad:function(options){ this.data.arry = JSON.parse(options.arry); //解析数组 } })

 

4、一次性传递多个数据、对象、数组

传递url传递多个数据的时候,使用 “&”来连接数据 Page({ data: { obj: {name:'我是name', extra:'我是extra'}, str: '字符串类型str', arry: ["1","2","3"] }, onLoad: function () { }, next: function(e){ wx.navigateTo({ url: '/pages/test/test?str='+this.data.str+'&obj='+JSON.stringify(this.data.obj)+'&arry='+JSON.stringify(this.data.arry) }) } }) 接收(所有传递过来的参数都在 options中可以得到) Page({ data:{ }, onLoad:function(options){ console.log("接收到的参数是str="+options.str); //如果使用JSON.parse(options.obj),打印出来不是字符串 console.log("接收到的对象是obj="+options.obj); //如果使用JSON.parse(options.arry),打印出来不是字符串 console.log("接收到的数组是arry="+JSON.parse(options.arry)); } })

 

使用缓存的方式 storage wx.setStorage(Object)来设置数据; wx.getStorage(Object)来获取数据。

以上是异步的,还有同步的方法。具体的使用方式还是去看api文档最合适。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3