Demo8.vue 542 B

12345678910111213141516171819202122232425
  1. <template>
  2. <div>
  3. <h1>Demo8:watch监听ref基本数据类型</h1>
  4. <h3>我有{{ flower }}朵花</h3>
  5. <button @click="changeFlower">减少</button>
  6. </div>
  7. </template>
  8. <script lang="ts" setup>
  9. import {ref,watch} from "vue"
  10. let flower = ref(20);
  11. function changeFlower() {
  12. flower.value--;
  13. }
  14. // 监听的是基本数据类型的值
  15. watch(flower,(newValue,oldValue) => {
  16. console.log(newValue,oldValue)
  17. },{
  18. deep: true, //开启深度监听
  19. immediate: true //立即监听
  20. })
  21. </script>
  22. <style lang="scss" scoped>
  23. </style>