天天看点

微信小程序wx.switchTab参数处理

官方文档上不能传递参数。

所以只能放在全局变量中。

app.js中

globalData: {
    homeCurrentTab:0
  }      

.wxml内容:navigator必须要有url参数

<navigator  bindtap='kind' url='../kind/kind' open-type="switchTab" data-id="0">
      <view ">跳转</view>
    </navigator>      

ind方法为:将data-id="0"中的数据作为全局参数保存getApp().globalData.homeCurrentTab

kind: function (e) {
   getApp().globalData.homeCurrentTab = e.currentTarget.dataset.id;
    console.log("kind", e.currentTarget.dataset.id);
  },      
onShow: function (e) {
    this.data.currentTab = getApp().globalData.homeCurrentTab;
    console.log("switch", this.data.currentTab)
    var self = this
    self.setData({
      currentTab: this.data.currentTab
    })
  },