前言

想类似Vue一样在小程序中绑定事件时直接传入参数,发现在小程序中行不通。最后通过dataset曲线救国。

代码

<view class="ys-classification">
  <a class="ys-card" href="javascript:;" bindtap="onItemClicked" data-index='{{index}}' wx:for='{{shop.categories}}' wx:key="item.id">
    <figure>
      <image src="{{item.image}}"></image>
      <figcaption>
        {{item.text}}
      </figcaption>
    </figure>
  </a>
</view>

js

  onItemClicked(event) {
    if ([1, 2].includes(event.currentTarget.dataset.index)) {
      wx.showToast({
        title: '暂未开通,敬请期待.',
        icon: 'none',
        duration: 800
      })
      return
    }else{
      console.log('Todo: goto product page.')
    }
  }

ps:注意使用的是currentTarget而不是target,这两者的区别可以百度。

Logo

前往低代码交流专区

更多推荐