Demo11.vue 740 B

1234567891011121314151617181920212223242526272829303132333435
  1. <template>
  2. <div>
  3. <h1>Watch:监听reactive引用数据类型</h1>
  4. <p>我叫{{ obj.name }},今年{{ obj.age }}岁</p>
  5. <button @click="changeName">修改名字</button>
  6. <button @click="changePerson">修改整体</button>
  7. </div>
  8. </template>
  9. <script lang="ts" setup>
  10. import {reactive,watch} from "vue"
  11. let obj = reactive({
  12. name:"图图",
  13. age:3
  14. })
  15. function changeName() {
  16. obj.name = '蜡笔小新';
  17. }
  18. function changePerson() {
  19. Object.assign(obj,{
  20. name:'瑶一瑶',
  21. age:4
  22. })
  23. }
  24. // 监听reactive引用数据类型 可以监听到属性
  25. watch(obj,(newValue,oldValue)=>{
  26. console.log(newValue,oldValue)
  27. },{
  28. deep:true,
  29. immediate: true
  30. })
  31. </script>
  32. <style lang="scss" scoped>
  33. </style>