2022-10-29 17:58阅读: 16评论: 0推荐: 0

监听属性

<!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>

本文作者:Sherwin

本文链接:https://www.cnblogs.com/sherwin1995/p/16839295.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Sherwin_szw  阅读(16)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.