shallowReadonly.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div>
  3. <h1 class="h-26 text-6xl">ShallowReadonly</h1>
  4. <!-- <h1 class="h-26 text-6xl">数值为:{{ sum.a }}--{{ sum.b.c }}</h1> -->
  5. <h1 class="h-26 text-6xl">数值1为:{{ sum1.a }}--{{ sum1.b.c }}</h1>
  6. <button
  7. class="w-30 h-10 text-center leading-[40px] border-2 border-grey-900 bg-grey-300"
  8. @click="changeSumA"
  9. >
  10. 修改
  11. </button>
  12. <button
  13. class="w-30 h-10 text-center leading-[40px] border-2 border-grey-900 bg-grey-300"
  14. @click="changeSumC"
  15. >
  16. 修改1
  17. </button>
  18. <button
  19. class="w-30 h-10 text-center leading-[40px] border-2 border-grey-900 bg-grey-300"
  20. @click="changeSum"
  21. >
  22. 修改2
  23. </button>
  24. </div>
  25. </template>
  26. <script lang="ts" setup>
  27. import { ref, reactive, readonly, shallowReadonly } from "vue";
  28. let sum = reactive({
  29. a: 20,
  30. b: {
  31. c: 40,
  32. },
  33. });
  34. let sum1 = shallowReadonly(sum);
  35. function changeSumA() {
  36. // sum.value.a = 50;
  37. // sum1.value.a = 1;
  38. sum1.a = 12;
  39. }
  40. function changeSumC() {
  41. // sum1.value.b.c = 2;
  42. // sum.value.b.c = 100;
  43. sum1.b.c = 12;
  44. }
  45. function changeSum() {
  46. let x1 = {
  47. a: 0,
  48. b: {
  49. c: 1,
  50. },
  51. };
  52. Object.assign(sum1, x1);
  53. }
  54. // function changeSum1() {
  55. // sum1.value = 60;
  56. // }
  57. </script>
  58. <style lang="scss" scoped>
  59. </style>