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 · 0 comments
Open

Vue3版本dom变化导致事件回调错乱 #5307

CX3201 opened this issue Feb 13, 2025 · 0 comments

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>
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

1 participant