js_reverse/学习VUE/hello_vue3/笔记/监视ref定义的数据.vue
2024-10-30 17:55:48 +08:00

36 lines
752 B
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="person">
<h2>当前求和{{sum}}</h2>
<button @click="changeSum">点击我sum+1</button>
</div>
</template>
<script setup>
import { ref, watch } from "vue";
let sum = ref(0)
//方法
function changeSum(){
sum.value += 1
}
//监视ref定义的数据,当你使用 watch 监听一个响应式的数据或 ref 时它返回一个函数这个函数被称为停止函数stop function。调用这个函数可以停止对该响应式数据的监听。
const stopWatch = watch(sum, (nValue, oValue)=>{
console.log(nValue, oValue)
if(nValue >= 10){
stopWatch()
}
})
</script>
<style scoped>
.person {
background-color: antiquewhite;
box-shadow: 0 0 10px;
}
button {
margin: 5px;
}
</style>