Skip to main content
vue3_watch

watch

  • 与vue2.x中watch配置一致

  • 监视reactive定义的响应式数据中某个属性时: deep配置有效

vue2写法

<template>
    <h1>求和:{{ sum }}</h1>
    <button @click="sum++">点我+1</button>
</tempalte>
<script>
    
    export default {
        watch: {
            //简写
            // sum(newValue, oldValue){
            //     console.log("sum变化了", newValue, oldValue);
            // }
            // 可配置属性
            sum: {
                immediate: true,    //立即监听
                deep: true,         //深度监听
                handler(newValue, oldValue){
                    console.log("sum变化了", newValue, oldValue);
                }
            }
        }
    }  
</script>

风不止About 1 minvuewatch