最近在做微信小程序時,涉及到了把值從一個頁面?zhèn)鬟f到另一個頁面的問題,在網(wǎng)上查閱了一些資料,在這里總結(jié)一下常用的方法。
1、頁面跳轉(zhuǎn)時,在跳轉(zhuǎn)的url中傳遞,比如:
-
wx.navigateTo({
-
url: '../InfoContent/InfoContent?id=1'
-
});
在InfoContent頁面:
-
onLoad: function (options) {
-
//頁面初始化 options為頁面跳轉(zhuǎn)所帶來的參數(shù)
-
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">跳轉(zhuǎn)到新頁面</button>
-
</view>
2、pagejump.js
-
Page({
-
data: {
-
lastval: {},
-
showBtn: false,
-
},
-
onLoad: function (options) {
-
// 生命周期函數(shù)--監(jiān)聽頁面加載
-
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) {
-
// 生命周期函數(shù)--監(jiān)聽頁面加載
-
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);
-
}
-
})
|