观察者的正确 Vue 3 <Setup Script/> 语法是什么?
What is proper Vue 3 <Setup Script/> syntax for Watchers?
观察者的正确 Vue 3 语法是什么?文档中似乎省略了它。我对这里看到的新功能感到非常兴奋:
https://vuejs.org/guide/essentials/watchers.html#deep-watchers
但是预期的语法是什么?
这是脚本设置语法里面的手表的基本示例:
<template>
<div>
<input type="text" v-model="user.name" placeholder="Name" />
<input type="text" v-model="user.lastname" placeholder="Lastname" />
{{ nameUpdatesCount }}
</div>
</template>
<script setup>
import { reactive, ref, watch } from "vue";
const user = reactive({ name: "", lastname: "" });
const nameUpdatesCount = ref(0);
const increaseNameUpdatesCount = () => {
nameUpdatesCount.value++;
};
watch(user, (newValue, oldValue) => {
console.log(newValue, oldValue);
increaseNameUpdatesCount();
});
</script>
在上面的示例中,每次您在输入中写入或删除某些内容时都会触发观察器。发生这种情况是因为名称 属性 更改了它的值。之后,调用 increaseNameUpdatesCount
方法,您将看到 nameUpdatesCount
加一。
观察者的正确 Vue 3 语法是什么?文档中似乎省略了它。我对这里看到的新功能感到非常兴奋: https://vuejs.org/guide/essentials/watchers.html#deep-watchers 但是预期的语法是什么?
这是脚本设置语法里面的手表的基本示例:
<template>
<div>
<input type="text" v-model="user.name" placeholder="Name" />
<input type="text" v-model="user.lastname" placeholder="Lastname" />
{{ nameUpdatesCount }}
</div>
</template>
<script setup>
import { reactive, ref, watch } from "vue";
const user = reactive({ name: "", lastname: "" });
const nameUpdatesCount = ref(0);
const increaseNameUpdatesCount = () => {
nameUpdatesCount.value++;
};
watch(user, (newValue, oldValue) => {
console.log(newValue, oldValue);
increaseNameUpdatesCount();
});
</script>
在上面的示例中,每次您在输入中写入或删除某些内容时都会触发观察器。发生这种情况是因为名称 属性 更改了它的值。之后,调用 increaseNameUpdatesCount
方法,您将看到 nameUpdatesCount
加一。