Demo10.vue 821 B

123456789101112131415161718192021222324252627282930313233343536
  1. <template>
  2. <div>
  3. <h1>Watch:监听ref引用数据类型</h1>
  4. <p>我在{{ obj.city }},现在是{{ obj.month }}</p>
  5. <button @click="changeCity">修改城市</button>
  6. <button @click="changeObj">修改整体</button>
  7. </div>
  8. </template>
  9. <script lang="ts" setup>
  10. import {ref,watch} from "vue"
  11. let obj = ref({
  12. city:'哈尔滨',
  13. month:"8月"
  14. })
  15. function changeCity() {
  16. obj.value.city = '北京'
  17. }
  18. function changeObj() {
  19. obj.value = {
  20. city:"天津",
  21. month:'9月'
  22. }
  23. }
  24. // 1.watch监听ref引用数据类型 监听的是整体 而不是单独的
  25. // 2.若监听ref引用数据类型中的属性 需要开启深度监听
  26. watch(obj,(newValue,oldValue)=>{
  27. console.log(newValue,oldValue)
  28. },{
  29. deep:true,
  30. immediate: true
  31. })
  32. </script>
  33. <style lang="scss" scoped>
  34. </style>