监听属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<body>
<div id="app">
    <input type="text" v-model="name">
</div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                name: 'szw'
            }
        }, watch: {
            //只要name发送变化,就会执行对应的函数
            name: function (val) {
                console.log(val)
            }
        }
    }).mount('#app')
</script>
</html>

组合式

<template>
    <div>
        <input type="text" v-model="age">
        <input type="text" v-model="age2">
    </div>
</template>

<script>
import { ref, computed, watch } from 'vue'
export default {
    setup() {
        let age = ref(0)
        let age2 = computed({
            get() {
                return +age.value + 1
            },
            set(value) {
                return age.value = value - 1
            }
        })
        watch([age,age2], (newValue,oldValue) => {
            console.log('老数据', oldValue)
            console.log('新数据', newValue)
        })
        return {
            age, age2
        }
    }
}
</script>
posted @ 2022-10-29 17:58  Sherwin_szw  阅读(13)  评论(0编辑  收藏  举报