Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【微信小程序】Vue3版本dom变化导致事件回调错乱 #5307

Open
CX3201 opened this issue Feb 13, 2025 · 7 comments
Open

【微信小程序】Vue3版本dom变化导致事件回调错乱 #5307

CX3201 opened this issue Feb 13, 2025 · 7 comments
Assignees

Comments

@CX3201
Copy link

CX3201 commented Feb 13, 2025

Vue3版本的事件回调是按顺序命名的,如果有v-if这些引起结构变化的场景,会导致两次渲染同一个命名的回调绑定到不同的回调函数上面,如果事件跨跃了这次渲染就会分发错误。

以下是复现代码,快速点击按钮会有概率回调到CB3:

<template>
  <div class="invisible" v-if="C1" @click="CB1"></div>
  <div class="button" @click="CB2">{{text}}</div>
  <div class="invisible" @click="CB3">CB3</div>
</template>
<script>
export default {
  data() {
    return {
      text: '快速点击',
      C1: true,
    };
  },
  mounted() {
    setInterval(() => {
      this.C1 = !this.C1;
    }, 300);
  },
  methods: {
    CB1() {
    },
    CB2() {
      this.text = '正确: 触发了CB2';
      console.log(this.text);
    },
    CB3() {
      this.text = '错误: 触发了CB3';
      console.log(this.text);
    },
  },
};
</script>
<style lang="less">
.invisible {
  height: 0px;
}
.button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  font-size: 80px;
  color: white;;
  background-color: red;
}
</style>
@GRCmade
Copy link
Collaborator

GRCmade commented Feb 25, 2025

hello ,我这里运行你的代码,尝试过很多次,点击红字都没有触发过 CB3 回调。还请你再详细说明一下问题。

@GRCmade GRCmade self-assigned this Feb 25, 2025
@CX3201
Copy link
Author

CX3201 commented Feb 25, 2025

hello ,我这里运行你的代码,尝试过很多次,点击红字都没有触发过 CB3 回调。还请你再详细说明一下问题。

你是用的哪个版本的uni-app?

@CX3201
Copy link
Author

CX3201 commented Feb 25, 2025

hello ,我这里运行你的代码,尝试过很多次,点击红字都没有触发过 CB3 回调。还请你再详细说明一下问题。

3.0.0-4040520250104002,刚试了一下在这个最新版本也是可以复现的,多点几次,点快一点

@CX3201
Copy link
Author

CX3201 commented Feb 25, 2025

hello ,我这里运行你的代码,尝试过很多次,点击红字都没有触发过 CB3 回调。还请你再详细说明一下问题。

忘了说明了,是微信小程序

@CX3201 CX3201 changed the title Vue3版本dom变化导致事件回调错乱 【微信小程序】Vue3版本dom变化导致事件回调错乱 Feb 25, 2025
@ZhenlyChen
Copy link

+1,遇到同样的问题

@GRCmade
Copy link
Collaborator

GRCmade commented Feb 25, 2025

感谢反馈,能够复现,具体问题还需要排查,暂时推荐使用 v-show 替代

@CX3201
Copy link
Author

CX3201 commented Feb 25, 2025

感谢反馈,能够复现,具体问题还需要排查,暂时推荐使用 v-show 替代

感觉是事件底层顺序机制的问题,如果事件跨越两次渲染就会有问题,目前我们采用了patch的方法,在vOn编译的时候全局计数生成编号,然后在事件名中附加这一编号,能临时解决这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants