微信小程序页面传值(父子传值)
1.父页面传值到子页面(只传简单的一个值)
//父页面方法
let str=1;
wx.navigateTo({
url: '../member/member_xingqing?usercard=' + str
})
//子页面接收
onLoad: function (options) {
console.log(options.usercard)
},
2.父页面传值到子页面(传多个值)
//父页面方法
let str=1;
let age=20;
wx.navigateTo({
url: '../member/member_xingqing?usercard=' + str+"&userage="+age
})
//子页面接收
onLoad: function (options) {
console.log(options.usercard)
console.log(options.userage)
},
3.父页面传值到子页面(传对象)
//父页面方法
let user:{
name:"小明",
age:"20",
work:"学习"
};
wx.navigateTo({
url: '../member/member_xingqing?usercard=' + JSON.stringify(user)
})
//子页面接收
onLoad: function (options) {
console.log(JSON.parse(options.item))
},
4.子页面给父页面传值
//父页面定义data下的接收参数:
data:{
name:'',
age:0,
like:'',
list:[]
}
//子页面方法中定义
subimt(){
let pagetotal = getCurrentPages();//获取盏队
parpage = pagetotal[pagetotal.length - 2];//父页面
//给父页面赋值
parpage.setData({
name:"王二小",
age:20,
like:'看电影',
list:[{like:"跑步",name:"小王"},{like:"跑步",name:"小王"},{like:"跑步",name:"小王"}]
})
//返回父页面
wx.navigateBack({//返回
delta: 1
})
}
父页面打印测试结果
onShow: function () {
console.log(this.data.name);//王二小
console.log(this.data.age);//20
console.log(this.data.like);//看电影
console.log(this.data.list)//[{like:"跑步",name:"小王"},{like:"跑步",name:"小王"},{like:"跑步",name:"小王"}]
},