Home >Web Front-end >Vue.js >How to use watch in Vue3
# The watch function is used to listen for changes in a certain value. When the value changes, the corresponding processing logic is triggered.
<template> <div> <div>{{ count }}</div> <button @click="changCount">更改count的值</button> </div> </template> <script setup> import {ref,reactive, watch} from 'vue' const count = ref(0) function changCount(){ count.value++ } watch(count,(newValue,oldValue)=>{ if(newValue){ console.log(`我侦听到了count状态的变化,当前值为${newValue},从而处理相关逻辑`); } }) </script> <style> </style>
<template> <div> <div>{{ x }}</div> <button @click="changCount">更改count的值</button> </div> </template> <script setup> import { ref, reactive, watch } from "vue"; const x = ref(1); const y = ref(5); function changCount() { x.value++; } watch( () => x.value + y.value, (sum) => { console.log(`我是x与y之和${sum}`); } ); </script> <style> </style>
<template> <div> <div>{{ x }}</div> <button @click="changCount">更改count的值</button> </div> </template> <script setup> import { ref, reactive, watch } from "vue"; const x = ref(1); const y = ref(5); function changCount() { x.value++; } watch( [x,y], ([x,y]) => { console.log(`我是x=>${x},我是y=》${y}`); } ); </script> <style> </style>
<template> <div> <div>{{ obj.name }}</div> <button @click="changObj">更改count的值</button> </div> </template> <script setup> import { ref, reactive, watch } from "vue"; const obj = ref({name:'你好'}) function changObj(){ obj.value.name+='我不好' } watch(()=>obj.value.name,(name)=>{ console.log(name); }) </script> <style> </style>
is used to enable deep monitoring
<template> <div> <div>{{ obj.name }}</div> <button @click="changObj">更改count的值</button> </div> </template> <script setup> import { ref, reactive, watch, watchEffect } from "vue"; const obj = ref({name:'你好'}) function changObj(){ obj.value.name+='我不好' } // obj是一个RefImpl对象,当不开启深度监听的时候,监听obj无法检测到obj属性的变化 watch(obj,()=>{ console.log(obj.value.name); }, { deep: true }) </script> <style> </style>
Whether to enable initialization detection. By default, the method in the listener will be executed only when the value changes. After immediate is enabled, initialization will be executed once.
<template> <div> <div>{{ obj.name }}</div> <button @click="changObj">更改count的值</button> </div> </template> <script setup> import { ref, reactive, watch, watchEffect } from "vue"; const obj = ref({name:'你好'}) function changObj(){ obj.value.name+='我不好' } // obj是一个RefImpl对象,当不开启深度监听的时候,监听obj无法检测到obj属性的变化 watch(obj,()=>{ console.log(obj.value.name); }, { deep: true,immediate:true }) </script> <style> </style>
It is common for listener callbacks to use exactly the same reactive state as the source. For example:
<template> <div> <div>{{ obj.name }}</div> <button @click="changObj">更改count的值</button> </div> </template> <script setup> import { ref, reactive, watch, watchEffect } from "vue"; const obj = ref({name:'你好'}) function changObj(){ obj.value.name+='我不好' } watch(obj.value,()=>{ console.log(obj.value.name); }) </script> <style> </style>
We can use the watchEffect function to simplify the above code. watchEffect()
Allows us to automatically track reactive dependencies of callbacks. The above listener can be rewritten as:
<template> <div> <div>{{ obj.name }}</div> <button @click="changObj">更改count的值</button> </div> </template> <script setup> import { ref, reactive, watch, watchEffect } from "vue"; const obj = ref({name:'你好'}) function changObj(){ obj.value.name+='我不好' } // watch(obj.value,()=>{ // console.log(obj.value.name); // }) watchEffect(()=>{ console.log(obj.value.name); }) </script> <style> </style>
Note: It should be noted that the watchEffect callback will be executed immediately, and there is no need to specify immediate
watch(source, callback, { flush: 'post' }) watchEffect(callback, { flush: 'post' })Stop listeningListeners created using synchronous statements in setup() or 5101c0cdbdc49998c642c71f6b6410a8 will be automatically bound to the host component instance, and Will automatically stop when the host component is uninstalled. Therefore, in most cases you don't need to worry about stopping a listener. One key point is that the listener must be created with a synchronous statement: if you create a listener with an asynchronous callback, then it will not be bound to the current component and you must stop it manually to prevent memory leakage. As an example below:
// ...当该侦听器不再需要时 unwatch()
The above is the detailed content of How to use watch in Vue3. For more information, please follow other related articles on the PHP Chinese website!