vant-ui swiperCell 需要觸發實例的open方法實現點擊出現滑動

需求:
有時候,需求就是這麼。。。。。。明明可以滑動顯示,他們就說,用戶不知道滑動,還需要手摸手的去教,好,這也是個不錯的理由。

要求,點擊swiper cell ,觸發滑動展示,

在這裏插入圖片描述

官方文檔說明,可以調用實例的open方法,傳參,‘left’ 'right’

發現傳參後,根本沒用效果。

查看官方ISSUS後,得到了解決辦法。

    handleClick(event) {
      event.stopPropagation();
      this.$refs["cell"].open("right");
    },

傳入事件對象,阻止事件冒泡,再配合open使用,就可以拉。

拓展: 很容易就可以實現點擊打開,再點擊,關閉的效果。

    handleClick(event) {
      event.stopPropagation();
      if (this.$refs["cell"].offset) {
        this.$refs["cell"].close();
        return;
      }
      this.$refs["cell"].open("right");
    },

繼續拓展: swiperCell是在v-for中渲染的,這個時候,需要動態的設置ref, 通過點擊的item來觸發對於的swiperCell的open()方法。

   <van-swipe-cell
              class="van-hairline--bottom"
              v-for="(item, index) in list"
              :ref="`list${item.id}`"
              :key="index"
            >
            <div @click="handleClick($event, item)">{{item.name}}</div>
</van-swipe-cell>

handleClick(event, item) {
      // console.log(event, item);
      event.stopPropagation();
      if (this.$refs[`list${item.id}`][0].offset) {
        this.$refs[`list${item.id}`][0].close();
        return;
      }
      this.$refs[`list${item.id}`][0].open("right");
    },

v-for, 動態設置refs,可以參考這篇文章:
https://blog.csdn.net/LzzMandy/article/details/91372406

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章