使用mpvue开发小程序,v-for新生成的元素上绑定的事件失效
发布于 5 年前 作者 yanxiulan 12597 次浏览 来自 问答

HTML代码如下:

<view
 
  v-show="!showLoading"
 
  v-for="(item, index) in cards"
 
  :key="index"
 
  class="body-swiper"
 
  @touchstart="touchstart"
 
  @touchend="touchend($event, index, item.content.user.id)"
 
  :style="{'z-index': (10 - index), top: item.top + 'px'}"
 
  :animation="item.animation">
 
    <!-- 不相关的内容 -->
 
</view>

js代码如下:

对ajax接口返回的数据做处理:

for(let i=0;i<res.data.list.length;i++) {
 
  // 有一些不相关的内容
 
  let item = {
 
    content: res.data.list[i],
 
    top: 18,
 
    animation: {}
 
  };
 
  this.cards.push(item);
 
  this.showLoading = false;
 
}

其中res.data.list中的数据是每次返回10条,

在初次进入页面时,请求接口的前10条数据循环生成的卡片上是有touchstarttouchend事件的,

之后的第二个10条中的数据循环生成的卡片元素上的事件就不起作用了

请问应该怎么改?

3 回复

你好,请问这个问题解决了吗?

你好,是 mpvue 的问题,请去 mpvue 的 github ub https://github.com/Meituan-Dianping/mpvue 提  issue。

每次点击重新加载页面数据

回到顶部