<template> <input type="text" v-model="text1" /> </template> <script setup> import { ref, watch } from 'vue' const text1 = ref('') watch(text1, (newVal, oldVal) => { console.log('监听单个数据', newVal, oldVal) }) </script>
<template> <input type="text" v-model="text1" /> <input type="text" v-model="text2" /> </template> <script setup> import { ref, watch } from 'vue' const text1 = ref('') const text2 = ref('') watch([text1, text2], (newVal, oldVal) => { console.log('监听一组数据', newVal, oldVal) }) </script>
<template> name: <input type="text" v-model="student.name" /> age: <input type="number" v-model="student.age" /> </template> <script setup> import { reactive, watch } from 'vue' const student = reactive({ name: '', age: '' }) watch(student, (newVal, oldVal) => { console.log('newVal', newVal) console.log('oldVal', newVal) }) </script>
watchThere is also a third parameter,
deep and
immediate, you can add it to see the effect
<template> name: <input type="text" v-model="student.name" /> age: <input type="number" v-model="student.age" /> </template> <script lang="ts" setup> import { reactive, watch } from 'vue' const student = reactive({ name: '', age: '' }) watch(() => student.name, (newVal, oldVal) => { console.log('newVal', newVal) console.log('oldVal', newVal) }, { deep: true, immediate: true }) </script>
When monitoring a certain property of an object, you need to use the arrow function
watchEffectAboutwatchEffect, The official website introduces it like this: In order to automatically apply and re-apply side effects based on the responsive state, we can use the
watchEffect method, which immediately executes an incoming function, while responsively tracking its dependencies and updating its dependencies. Rerun this function when changed. In other words, we do not need to pass in a specific dependency source, and it will immediately execute the callback function. If the function produces side effects, it will automatically track the dependencies of the side effects and automatically analyze the response source. The concept may be vague just by looking at it. Let’s take a look at the simplest example first:
<template> name: <input type="text" v-model="student.name" /> age: <input type="number" v-model="student.age" /> </template> <script lang="ts" setup> import { reactive, watchEffect } from 'vue' const student = reactive({ name: '', age: '' }) watchEffect(() => { console.log('name: ',student.name, 'age: ', student.age) }) </script>
<template> name: <input type="text" v-model="student.name" /> age: <input type="number" v-model="student.age" /> <h3>{{student.name}}</h3> </template> <script lang="ts" setup> import { reactive, watchEffect } from 'vue' const student = reactive({ name: '', age: '' }) watchEffect((oninvalidate) => { oninvalidate(() => { student.name = '张三' }) console.log('name: ', student.name) }, { // pre 组件更新前; sync:强制效果始终同步; post:组件更新后执行 flush: 'post' // dom加载完毕后执行 }) </script>
student.name be assigned the value 'Zhang San'. No matter what value you enter,
name will always be 'Zhang San'
watch or
watchEffect
const stop = watchEffect(() => {}) // 停止监听 unwatch()
After watch and
watchEffect, I found that they have the following main differences:
watch is executed lazily, while
watchEffect No, without considering the third configuration parameter of
watch,
watch will not be executed when the component is executed for the first time, only after It will be executed when the dependencies change, while
watchEffect will be executed immediately when the program is executed here, and then executed in response to its dependency changes.
watchYou need to pass the monitoring object,
watchEffectNot required
The above is the detailed content of Analysis of usage examples of watch and watchEffect in vue3. For more information, please follow other related articles on the PHP Chinese website!