Skip to content

有关watch触发的一个问题 #11749

Discussion options

You must be logged in to vote

template 是函数环境 render,每次修改 input 的时候,会触发 render,然后每次执行render都会生成新的 {a:1,b:2} 此时创建的 obj 和上一次执行render 时不一样,所以触发了 watch,但是这种场景似乎只会在 vue2中出现,我在 playground 中并未发现这一问题。

您可以暂时将obj声明在 setup 中,避免每次render生成新的obj:

<template>
  <HelloWorld v-model:value="model" :obj="obj" />
</template>
<script setup>
import { ref } from 'vue';
import HelloWorld from './components/HelloWorld.vue';
const model = ref('');
const obj = { a: 1, b: 2 };
</script>

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@lih21
Comment options

@Alfred-Skyblue
Comment options

Answer selected by lih21
@lih21
Comment options

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