Vue 监听属性(侦听器)

这里结合 天气案例 来理解 Vue 监听属性


天气案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>天气案例</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
    <h2>天气:{{info}}</h2>
    <button @click="changeWeather">切换天气</button>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#root',
        data: {
            isHot: true
        },
        computed: {
            info() {
                return this.isHot ? '热' : '冷'
            }
        },
        methods: {
            changeWeather() {
                this.isHot = !this.isHot
            }
        }
    })
</script>
</html>


天气案例_监听属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>天气案例_监视属性</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
    <h2>天气:{{info}}</h2>
    <button @click="changeWeather">切换天气</button>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#root',
        data: {
            isHot: true
        },
        computed: {
            info() {
                return this.isHot ? '热' : '冷'
            }
        },
        methods: {
            changeWeather() {
                this.isHot = !this.isHot
            }
        },
        watch: {
            isHot: {
                // 初始化时让 handler 调用一下
                immediate: false,
                // 当 isHot 发生改变时 handler 被调用
                handler(newValue, oldValue) {
                    console.log('isHot 被修改', newValue, oldValue)
                }
            }
        }
    })
</script>
</html>

使用 watch 选项或者 vm.$watch

选择需要监听的对象 isHot 配置 handler(newValue, oldValue)

其中 newValue 为变化后的值 oldValue 为变化前的值


备注:immediate: true 会让 handler 在初始化时被调用一下



天气案例_监听属性_简写

简写的代价:不能配置immediate: true 等配置类


实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>天气案例_监视属性_简写</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
    <h2>天气:{{info}}</h2>
    <button @click="changeWeather">切换天气</button>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#root',
        data: {
            isHot: true
        },
        computed: {
            info() {
                return this.isHot ? '热' : '冷'
            }
        },
        methods: {
            changeWeather() {
                this.isHot = !this.isHot
            }
        },
        watch: {
            //简写
            isHot(newValue, oldValue) {
                console.log('isHot 被修改')
            }
        }
    })
</script>
</html>

使用 watch 选项 简写 示例

// 正常写法
isHot: {
	handler(newValue, oldValue) {
		console.log('isHot 被修改')
		}
	}
//简写
isHot(newValue, oldValue) {
	console.log('isHot 被修改')
	}
}

使用 vm.$watch 简写 示例

// 正常写法
vm.$watch('isHot', {
    handler(newValue, oldValue) {
        console.log('isHot 被修改')
    }
})
// 简写
vm.$watch('isHot', function (newValue, oldValue) {
    console.log('isHot 被修改')
})

posted @ 2022-04-06 13:08  春暖花开鸟  阅读(138)  评论(0编辑  收藏  举报