shallowRef-shallowReactive.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <div>
  3. <h1>shallowRef-shallowReactive</h1>
  4. <p>sum:{{ sum }}</p>
  5. <p>sum1:{{ sum1 }}</p>
  6. <p>第一个:{{ car.name }}--{{ car.color }}--{{ car.options.c1 }}</p>
  7. <p>第二个:{{ car1.name }}--{{ car1.color }}--{{ car1.options.c1 }}</p>
  8. <button @click="changeMain1">修改1</button>
  9. <button @click="changeMain2">修改2</button>
  10. </div>
  11. </template>
  12. <script lang="ts" setup>
  13. import { ref, reactive, shallowRef, shallowReactive } from "vue";
  14. let sum = ref(10);
  15. let sum1 = shallowRef(20);
  16. let car = reactive({
  17. name: "自行车",
  18. color: "白",
  19. options: {
  20. c1: "1",
  21. },
  22. });
  23. let car1 = shallowReactive({
  24. name: "自行车",
  25. color: "白",
  26. options: {
  27. c1: "1",
  28. },
  29. });
  30. function changeMain1() {
  31. // sum.value = 100;
  32. // car.name = '你好'
  33. // car.options.c1 = '12'
  34. // car = {
  35. // name: "自行车1",
  36. // color: "白1",
  37. // options: {
  38. // c1: "11",
  39. // },
  40. // };
  41. Object.assign(car, {
  42. name: "自行车1",
  43. color: "白1",
  44. options: {
  45. c1: "11",
  46. },
  47. });
  48. }
  49. function changeMain2() {
  50. // sum1.value = 200;
  51. // car1.name = '你好1'
  52. // car1.options.c1 = '122222'
  53. // car1.value = {
  54. // name: "自行车12",
  55. // color: "白12",
  56. // options: {
  57. // c1: "112",
  58. // },1
  59. // };
  60. Object.assign(car1, {
  61. name: "自行车12",
  62. color: "白12",
  63. options: {
  64. c1: "112",
  65. },
  66. });
  67. }
  68. </script>
  69. <style lang="scss" scoped>
  70. </style>