天天看點

微信小程式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
    })
  },