Demo9.vue 716 B

1234567891011121314151617181920212223242526272829303132
  1. <template>
  2. <div>
  3. <h1>Demo9:watch监听ref引用数据类型</h1>
  4. <h3>我有{{ flower.type }}花,是{{ flower.color }}</h3>
  5. <button @click="changeFlowerColor">颜色</button>
  6. <button @click="changeFlower">整体</button>
  7. </div>
  8. </template>
  9. <script lang="ts" setup>
  10. import {ref,watch} from "vue"
  11. let flower = ref({
  12. color:"红色",
  13. type:"牡丹"
  14. });
  15. function changeFlowerColor() {
  16. flower.value.color = '粉色'
  17. }
  18. function changeFlower() {
  19. flower.value = {type:"丁香花",color:"紫色"}
  20. }
  21. // 监听的是基本数据类型的值
  22. watch(flower,(newValue,oldValue) => {
  23. console.log(newValue,oldValue)
  24. },{
  25. deep: true,
  26. immediate: true
  27. })
  28. </script>
  29. <style lang="scss" scoped>
  30. </style>