App.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <h2>shallowReactive 与 shallowRef</h2>
  3. <h3>m1: {{ m1 }}</h3>
  4. <h3>m2: {{ m2 }}</h3>
  5. <h3>m3: {{ m3 }}</h3>
  6. <h3>m4: {{ m4 }}</h3>
  7. <hr>
  8. <button @click="update">更新数据</button>
  9. </template>
  10. <script lang="ts">
  11. import { defineComponent, reactive, shallowReactive,ref, shallowRef } from "vue";
  12. export default defineComponent({
  13. setup() {
  14. //深度劫持(深监视) ------深度响应式
  15. const m1 = reactive({
  16. name: 'xiaoming',
  17. firend: {
  18. name: 'xiaohong'
  19. }
  20. })
  21. //浅度响应式 只处理了对象内最外层的属性响应式
  22. const m2 = shallowReactive({
  23. name: 'xiaoming',
  24. firend: {
  25. name: 'xiaohong'
  26. }
  27. })
  28. //深度
  29. const m3 = ref({
  30. name: 'xiaoming',
  31. firend: {
  32. name: 'xiaohong'
  33. }
  34. })
  35. //shallowRef处理了value的响应式 不进行对象的reative处理
  36. const m4 = shallowRef({
  37. name: 'xiaoming',
  38. firend: {
  39. name: 'xiaohong'
  40. }
  41. })
  42. const update = ()=>{
  43. //更改m1的数据---reactive
  44. // m1.name += '='
  45. // m1.firend.name += '!'
  46. //更改m2的数据---shallowReactive
  47. // m2.name += '='
  48. // m2.firend.name += '!'
  49. //更改m3的数据---ref
  50. // m3.value.name += '='
  51. // m3.value.firend.name += '!'
  52. //更改m4的数据---shallowRef
  53. // m4.value.name += '='
  54. console.log(m3,m4)
  55. }
  56. return {
  57. m1,
  58. m2,
  59. m3,
  60. m4,
  61. update
  62. };
  63. },
  64. });
  65. </script>
  66. <style scoped>
  67. </style>