Demo16.vue 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <div>
  3. <h1>shallowRef与shallowReactive</h1>
  4. <h2>Ref:{{ cc.name }}--{{ cc.age }}--{{ cc.x.a }}</h2>
  5. <h2>shallowRef:{{ dd.name }}--{{ dd.age }}--{{ dd.x.a }}</h2>
  6. <button @click="handleChange">修改</button>
  7. </div>
  8. </template>
  9. <script lang="ts" setup>
  10. import { ref, reactive, shallowRef, shallowReactive } from "vue";
  11. let aa = ref(1);
  12. let bb = shallowRef(33);
  13. let cc = reactive({
  14. name: "图图",
  15. age: 3,
  16. x: {
  17. a: 1,
  18. },
  19. });
  20. let dd = shallowReactive({
  21. name: "小明",
  22. age: 13,
  23. x: {
  24. a: 3,
  25. },
  26. });
  27. const handleChange = () => {
  28. Object.assign(dd, {
  29. name: 1,
  30. age: 2,
  31. x: { a: 3 },
  32. });
  33. // cc.name = "孙悟空";
  34. // dd.value.x.a = 11;
  35. // dd.value = {
  36. // name: "猪八戒",
  37. // age: 4,
  38. // x: { a: 99 },
  39. // };
  40. // dd.value.name = "12";
  41. // aa.value++;
  42. // bb.value++;
  43. };
  44. </script>
  45. <style lang="scss" scoped>
  46. </style>