Demo6.vue 947 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <template>
  2. <div>
  3. <p>
  4. 我叫{{ obj.name }}今年{{ obj.age }}岁,我喜欢{{ obj.color.c1 }}和{{
  5. obj.color.c2
  6. }}
  7. </p>
  8. <button @click="changePart1">修改名字</button>
  9. <button @click="changePart2">修改整体</button>
  10. <button @click="changePart3">修改颜色一</button>
  11. <button @click="changePart4">修改整体颜色</button>
  12. </div>
  13. </template>
  14. <script lang="ts" setup>
  15. import { ref, reactive } from "vue";
  16. let obj = reactive({
  17. name: "小明",
  18. age: 10,
  19. color: {
  20. c1: "红色",
  21. c2: "黄色",
  22. },
  23. });
  24. function changePart1() {
  25. obj.name = "图图";
  26. }
  27. function changePart2() {
  28. Object.assign(obj, {
  29. name: "小明1",
  30. age: 101,
  31. color: {
  32. c1: "红色1",
  33. c2: "黄色1",
  34. },
  35. });
  36. }
  37. function changePart3() {
  38. obj.color.c1 = "yellow";
  39. }
  40. function changePart4() {
  41. obj.color = {
  42. c1: "1",
  43. c2: "2",
  44. };
  45. }
  46. </script>
  47. <style lang="scss" scoped>
  48. </style>