微信小程序之跳转tabbar页面相关问题
-
使用wx.switchTab跳转
写一个跳转到tabbar页面,使用wx.navigateTo来实现发现页面没有跳转,在微信官方文档上发现只能使用wx.switchTab进行跳转到tabbar相关页面。
跳转tabbar页面传参问题
但是发现wx.switchTab不能通过url传参数所以只能通过定义全局变量来解决。
-
全局变量的设置和使用
在需要跳转的页面js中: getApp().globalData.infoList=this.data.infoList
将infoList设置成了全局变量,this.data.infoList就是将这个页面的infoLIst值修改全局变量infoLIst。
getApp().globalData.infoList=this.data.infoList
wx.switchTab({
url:'../index/index',
success: function(res){
console.log('跳转页面成功');//success
},
fail: function() {
console.log('跳转页面失败');// fail
},
complete: function() {
console.log('跳转页面完成');// complete
}
在index.js的onLoad中
var infoList=getApp().globalData.infoList;
console.log(infoList);
输出的结果是undefined,这是因为没有在app.js定义全局变量infoList。
在app.js中
App({
onLaunch: function () {
},
globalData: {
infoList:null,
}
})
globalData对象就是存储全局变量的。
- 跳转之后页面刷新
由于index页面在前面加载了,再通过跳转到index导致页面没有刷新,数据就没有加载出来,所以需要刷新页面。
详细信息转载于:https://blog.csdn.net/ba12313/article/details/101701184
wx.switchTab({
url:'../index/index',
success: function(res){
//刷新页面
var page = getCurrentPages().pop();
if (page == undefined || page == null)return;
page.onLoad();
console.log('跳转页面成功');//success
},
fail: function() {
console.log('跳转页面失败');// fail
},
complete: function() {
console.log('跳转页面完成');// complete
}
})
这样就可以在index.js中获取infoList的数据。