微信小程序之跳转tabbar页面相关问题

微信小程序之跳转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对象就是存储全局变量的。

   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的数据。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子时不睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值