watch
: Listen to data changes (change event of a certain value)
data(){ return{ num:10 } }, watch:{ num:{ /* * newValue: current value * oldValue: Modify the value of the previous moment */ handler(newValue,oldValue){ // doSomething }, /* * deep:Boolean: Deep monitoring * true: If the listening heap changes are * false: only listen for changes to the stack (default) */ deep:true/false, /* * immediate:Boolean: Whether to execute handler function when the first definition is defined * true: Execute handler function when the first definition is * false: After modifying, execute the handler function */ immediate:true/false } }
Watch is used to listen to responsive data
Basic use
const num = ref(0) 1. Import import {watch} from 'vue' 2. use `const Return value= watch(Values that need to be monitored, (newVal,oldVal)=>{ }, {deep,immediate,flush})` Return value: You can turn off the monitoring: Return value() Parameter 1: Values that need to be monitored Basic data types(Number,String,Boolean,null,undefined): ()=>Basic data types值 Complex data types(Array,Object,Function): Write directly/()=>Basic data types值 Parameter 2: analogyVue2In-househandlerfunction Parameter Three: {}Object, Object中可以有个配置项:deep,immediate,flush, deep,immediateThe meaning is described above, Here is the main oneflushExplanation of the value of: `flush:post/sync/pre pre(default value):Before rendering,The value has changed,No renderingdom post:After rendering,The value has changed,Also rendereddom sync:Change once rendering once,每一次都是Before rendering`
Note:
No changes are heard during actual development. Use uniformly
watch(()=>responsive data,()=>{},{deep:true})
The above is the detailed explanation of how to use the watch listener for Vue2 and Vue3. For more information about the use of the watch listener, please follow my other related articles!