Demo12.vue 855 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <template>
  2. <div>
  3. <h1>Demo12:watch监听reactive定义的引用数据类型</h1>
  4. <h2>我在{{ flower.month }}的{{ flower.city }}--{{flower.a.b }}</h2>
  5. <button @click="changeMonth">修改月份</button>
  6. <button @click="changeB">修改b</button>
  7. <button @click="changeFlower">修改整体</button>
  8. </div>
  9. </template>
  10. <script lang="ts" setup>
  11. import { reactive, watch } from "vue";
  12. let flower = reactive({
  13. month: "六月",
  14. city: "哈尔滨",
  15. a:{
  16. b:1
  17. }
  18. });
  19. function changeB() {
  20. flower.a.b = 12;
  21. }
  22. function changeFlower() {
  23. // flower.value--;
  24. Object.assign(flower, {
  25. month: "八月",
  26. city: "北京"
  27. });
  28. }
  29. function changeMonth() {
  30. flower.month = "七月";
  31. }
  32. watch(
  33. flower,
  34. (newValue, oldValue) => {
  35. console.log(newValue, oldValue, "12");
  36. }
  37. );
  38. </script>
  39. <style lang="scss" scoped>
  40. </style>