小程序Storage正确使用方法?

下面是nav组件代码

<view class='wrap'>
  <view class='ul'>
    <view class='li' wx:for="{{urlList}}"  wx:key="key" wx:for-index="idx">
      <navigator  data-active="{{idx}}" bindtap="onTap" redirect="true"  open-type="navigate"
      class='item {{active == idx ? "other-navigator-hover" : ""}}'  
      url='{{"./../" + item.url + "/" + item.url}}'>{{item.txt}}</navigator>
    </view>
  </view>
</view>

这里是nav组件Js

  properties: {
    active: Number
  },
  methods: {
    onTap: function (e) {
      wx.setStorageSync("navActive", e.target.dataset.active)
      console.log(wx.getStorageSync('navActive'))
    }
}

下面是页面代码了

  <topNav active="{{topNavActive}}"></topNav> 

页面JS

 data: {
    topNavActive:null
  }

onLoad: function () {
    let that = this
    let acNum = wx.getStorageSync("navActive")
    that.setData({
      topNavActive: acNum
    })
  }

在开发者工具上面active没问题,在手机上调试就出现问题了,getStorage的时候回出现get到上次的数字导致active class 还是回到上个导航那里。

阅读 2.8k
1 个回答

已找到解决方案,不能用导航标签,直接用view绑定方法跳转。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题