微信小程序wx.switchTab

【转】关于微信小程序wx.switchTab的问题php

昨天作了个功能要从首页跳到tabBar页,而且要带上参数.首先我是这样作的:html

在index.js中:json

toCategory:function(event){
    var cate_id = event.currentTarget.dataset.cate_id;
    wx.switchTab({
      url: '../category/category?cate_id='+cate_id,
    });
  },
小程序

按照上面写的在category.js里得不到数据;微信小程序

onLoad:function(options){微信

console.log(options);app

}函数

而后查看了下官方文档.this

 

wx.switchTab(OBJECT)

 

跳转到 tabBar 页面,并关闭其余全部非 tabBar 页面url

OBJECT 参数说明:

参数 类型 必填 说明
url String 须要跳转的 tabBar 页面的路径(需在 app.json 的 tabBar 字段定义的页面),路径后不能带参数
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
能够看到url路径后不能带参数.这下就尴尬了.由于我必需要带一个参数过去才能知足业务的需求;走了一个捷径.思路:跳转的时候在全局变量里设置一个变量cate_id,调到category.js中后.调取全局变量里的cate_id,用完后,再把扎个变量清除掉.具体实施以下:

首页index.js中:

toCategory:function(event){
    var cate_id = event.currentTarget.dataset.cate_id;
    app.globalData.cate_id=cate_id;//设置全局变量(app已经定义 var app=getApp())
    wx.switchTab({
      url: '../category/category'
    });
  },

分类页category.js中:

onLoad:function(options){
    var that = this
    var cate_id=app.globalData.cate_id
    wx.request({
      url: app.globalData.httpsurl +'public/index.php?s=product/index',
      data:{
        cate_id:cate_id,
      },
      success:function(res){
        //清除全局变量cate_id
        app.globalData.cate_id=""
        that.setData({
          alldata:res.data,
        })
      }
    })
  },

上面的转化就能够完成业务逻辑的须要了.

相关文章
相关标签/搜索