shallowReactive浅层式响应对象

一、

 reactive 和ref 都是深层响应式对象: 就是不管对象有多少层,修改此对象任一属性都会响应式处理

 shallowReactive 和shallowRef 浅层响应式对象: 只会修改第一层对象,修改此对象第一层属性,视图会有同步变化,非第一层,数值会变,视图不会变。

例如有这样一个对象

{
id:1,
 name:'张三',
 car:{
     price: 7000,
     color: 'red'
 }
}

vue3中定义shallowReactive 对象后,修改id。视图会同步变化,如果修改的是car.price。视图不会变化,除非先修改car.price 对象,然后再修改id,这时第一层对象触发,会把这个对象更新。具体例子如下:

<script setup> 
  /**
   * reactive 和ref 都是深层响应式对象: 就是不管对象有多少层,修改此对象任一属性都会响应式处理
   * shallowReactive 和shallowRef 浅层响应式对象:
   * 
   * 
   */
  import {reactive,ref,shallowReactive} from 'vue';

  const state = reactive({
    id:1,
    name:'张三',
    car:{
      price: 7000,
      color: 'red'
    }
  });

  function updateStateId() {
    state.id++;
  };
  function updateStatePrice() {
    state.car.price++;
  };


  const stateRef = ref({
    id:1,
    name:'张三',
    car:{
      price: 7000,
      color: 'red'
    }
  });

  function updateRefStateId() {
    stateRef.value.id++;
  };
  function updateRefStatePrice() {
    //直接改非第一层数据,视图不更新,也就是多层级的数据是非响应式的
    stateRef.value.car.price++;
  };


  const shallowstate = shallowReactive({
    id:1,
    name:'张三',
    car:{
      price: 7000,
      color: 'red'
    }
  });

  function updateIdByShallowReactive() {
    shallowstate.id++;
  };

  function updatePriceByShallowReactive() {
    //直接改非第一层数据,视图不会更新,也就是多层级的数据是非响应式的
    shallowstate.car.price++;
  };

  function updatePriceAndIdByShallowReactive() {
    //直接改非第一层数据,视图不会更新,也就是多层级的数据是非响应式的
    shallowstate.car.price++;
    //当修改了第一层数据,也修改其他层数据,此时会将此对象所有的数据都更新视图
    //原理:当改变底层数据会触发该状态的监听器,将此状态所有数据更新到视图中
    shallowstate.id++;
  };

</script>

<template>
 
 <div>
    <p>reactive=={{ state.id }}=={{ state.car }}</p>
    <button @click="updateStateId">更新reactive</button>
    <button @click="updateStatePrice">更新reactiveprice</button>

    <p>ref=={{ stateRef.id }}=={{ stateRef.car }}</p>
    <button @click="updateRefStateId">更新ref</button>
    <button @click="updateRefStatePrice">更新ref price</button>
    <h4>function updatePriceAndIdByShallowReactive() {<br>
    //直接改非第一层数据,视图不会更新,也就是多层级的数据是非响应式的<br>
    shallowstate.car.price++;<br>
    //当修改了第一层数据,也修改其他层数据,此时会将此对象所有的数据都更新视图<br>
    shallowstate.id++;<br>
  };</h4>
    <p>shallowReactive=={{ shallowstate.id }}=={{ shallowstate.car }}</p>
    <button @click="updateIdByShallowReactive">shallowReactive更新id</button>
    <button @click="updatePriceByShallowReactive">shallowReactive更新car.price</button>
    <button @click="updatePriceAndIdByShallowReactive">shallowReactive更新car.price和id</button>

 </div>
</template>

<style scoped>

</style>

点击更新ref对象数据id++ 按钮,id属性加1

点击更新ref对象数据car.price++,car.price属性加1

点击更新reactive对象数据id++按钮,id属性加1

点击更新reactive对象数据car.price++,car.price属性加1

点击更新shallowReactive对象属性id++,id属性加1

点击更新shallowReactive对象属性car.price++,car.price属性加1,视图不更新。还是7000

点击更新shallowReactive对象属性car.price和id++,car.price和id属性都加1,并且shallowReactive视图更新。看到下图id加了1,price 加了2。

相关推荐

  1. 谈vue响应

    2024-03-18 05:28:02       54 阅读
  2. 谈Vue 3的响应对象: ref和reactive

    2024-03-18 05:28:02       39 阅读
  3. 谈 Socket.D 与响应编程

    2024-03-18 05:28:02       77 阅读
  4. 第16节:Vue3 响应对象reactive()

    2024-03-18 05:28:02       58 阅读
  5. vue2对象丢失响应解决办法

    2024-03-18 05:28:02       62 阅读
  6. 关于直接赋值对象导致响应丢失

    2024-03-18 05:28:02       46 阅读
  7. 【Vue3】reactive对象类型的响应数据

    2024-03-18 05:28:02       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 05:28:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 05:28:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 05:28:02       82 阅读
  4. Python语言-面向对象

    2024-03-18 05:28:02       91 阅读

热门阅读

  1. 【Unity入门】详解Unity中的射线与射线检测

    2024-03-18 05:28:02       31 阅读
  2. 高等代数复习:应试经验:求行列式

    2024-03-18 05:28:02       43 阅读
  3. 24计算机考研调剂 | 武汉科技大学

    2024-03-18 05:28:02       38 阅读
  4. Go语言-关于 go get 和 go install

    2024-03-18 05:28:02       42 阅读
  5. 新一代云原生数据库OLAP

    2024-03-18 05:28:02       41 阅读
  6. 微软 CEO Satya Nadella 的访谈

    2024-03-18 05:28:02       31 阅读
  7. C++/CLI学习笔记5(快速打通c++与c#相互调用的桥梁)

    2024-03-18 05:28:02       42 阅读
  8. Vue3项目随笔

    2024-03-18 05:28:02       38 阅读
  9. [C++] 实现Union

    2024-03-18 05:28:02       43 阅读