Demo8.vue 603 B

12345678910111213141516171819202122232425262728293031
  1. <template>
  2. <div>
  3. <div>Watch1.0</div>
  4. <!-- watch监听ref基本数据类型 -->
  5. <h3>我送了{{ flower }}花</h3>
  6. <button @click="sendFlower">再送一朵</button>
  7. </div>
  8. </template>
  9. <script lang="ts" setup>
  10. import { watch, ref } from "vue";
  11. let flower = ref(10);
  12. function sendFlower() {
  13. flower.value += 1;
  14. }
  15. /***
  16. * watch
  17. * 1.监听对象
  18. * 2.回调函数
  19. * 3.深度监听
  20. */
  21. watch(flower,(newValue,oldValue)=>{
  22. console.log(newValue,oldValue)
  23. },{
  24. deep: true,//开启深度监听
  25. immediate: true // 立即执行
  26. })
  27. </script>
  28. <style lang="scss" scoped>
  29. </style>