最近在做微信小程序时,涉及到了把值从一个页面传递到另一个页面的问题,在网上查阅了一些资料,在这里总结一下常用的方法。
1、页面跳转时,在跳转的url中传递,比如:
-
wx.navigateTo({
-
url: '../InfoContent/InfoContent?id=1'
-
});
在InfoContent页面:
-
onLoad: function (options) {
-
//页面初始化 options为页面跳转所带来的参数
-
var s = this;
-
var id=options.id;//获取值
-
}
2、全局变量的形式 app.js代码:
-
globalData:{
-
id: 1
-
}
赋值代码:
-
var app = getApp();
-
app.globalData.id=2
取值代码:
-
var app = getApp();
-
var id=app.globalData.id;
1、pagejump.wxml:
-
<view class="page">
-
-
<button type="default" bindtap="onBtnClick">跳转到新页面</button>
-
</view>
2、pagejump.js
-
Page({
-
data: {
-
lastval: {},
-
showBtn: false,
-
},
-
onLoad: function (options) {
-
// 生命周期函数--监听页面加载
-
var that = this;
-
console.log('onLoad is invoked');
-
console.log(options);
-
that.setData({
-
lastval: options.val,
-
-
})
-
},
-
onBtnClick: function () {
-
var that = this;
-
console.log('onBtnClick');
-
wx.navigateTo({
-
url: '../home/home?tp=2&index=hello bright&showBtn=false',
-
success: function (res) {
-
// success
-
console.log('onBtnClick success() res:');
-
},
-
fail: function () {
-
// fail
-
console.log('onBtnClick fail() !!!');
-
},
-
complete: function () {
-
console.log('onBtnClick complete() !!!');
-
// complete
-
}
-
})
-
}
-
})
3、home.wxml:
-
<view class="test">新页面新页面tp=={{tp}}</view>
-
<view class="test">新页面新页面showBtn=={{showBtn}}</view>
-
<view class="test">新页面新页面index=={{index}}</view>
4、home.js:
-
Page({
-
data: {
-
tp:'',
-
showBtn: '',
-
index:'',
-
},
-
onLoad: function (options) {
-
// 生命周期函数--监听页面加载
-
var that = this;
-
console.log('onLoad is invoked');
-
that.setData({
-
tp: options.tp,
-
index: options.index,
-
showBtn: (options.showBtn == "true" ? true : false),
-
});
-
console.log("tp="+tp);
-
}
-
})
|