vue3 ref,shallowRef,reactive,shallowReactive使用的简单异同点说明

1、这几个都是负责data的存储及读取的,我们常用的是ref,reactive。

2、看一下shallowRef这个,shallowReactive与其类似。

说明:以官网的说明,这个state.value.count = 2是不会触发视图更新的,但是如果直接在<script setup lang="ts"></script>中直接修改的时候,你会发现这个数据是更改了,并且视图也会更新,并不是原始值。

但是如果在onMounted里写,那么这个视图是不会被更新的。

3、那么我们增加一个按钮事件add来触发下,看看效果:

我们会发现,如果只是修改shallowRef的nested.count++,那么也是不会被更新。

有一点先重要,我们把上面的二行注解去掉,add中会同时去更新ref的变量值,再来看看效果:

4、完整代码:

<template>
  <div>
    <h1>Ref</h1>
    {{ stateref }}
    <h1>Reactive</h1>
    {{ statereactive }}
    <h1>ShallowRef</h1>
    {{ state }}
    <input type="text" v-model="state.greet" />
    <h1>ShallowReactive</h1>
    {{ stater }}
    <br />
    <br />
    <button @click="add">add</button>
  </div>
</template>
<script setup lang="ts">
import {
  onMounted,
  reactive,
  ref,
  shallowReactive,
  shallowRef,
  unref,
} from "vue";
const statereactive = reactive({
  count: 1,
  greet: "Hello, world",
  nested: { count: 1 },
  list: [
    {
      id: 1,
      name: "a",
    },
    {
      id: 2,
      name: "b",
    },
  ],
});
const stateref = ref({
  count: 1,
  greet: "Hello, world",
  nested: { count: 1 },
  list: [
    {
      id: 1,
      name: "a",
    },
    {
      id: 2,
      name: "b",
    },
  ],
});
const stater = shallowReactive({
  count: 1,
  greet: "Hello, world",
  nested: { count: 1 },
  list: [
    {
      id: 1,
      name: "a",
    },
    {
      id: 2,
      name: "b",
    },
  ],
});
const state = shallowRef({
  count: 1,
  greet: "Hello, world",
  nested: { count: 1 },
  list: [
    {
      id: 1,
      name: "a",
    },
    {
      id: 2,
      name: "b",
    },
  ],
});

onMounted(() => {
  // stateref.value.count = 2;
  // stateref.value.greet = "Hello, Vue 3";
  // stateref.value.nested.count = 2;
  // stateref.value.list[0].name = "c";
  // console.log(JSON.stringify(unref(stateref))); // 2

  // statereactive.count = 2;
  // statereactive.greet = "Hello, Vue 3";
  // statereactive.nested.count = 2;
  // statereactive.list[0].name = "c";
  // console.log(JSON.stringify(unref(statereactive))); // 2

  // 不会触发更改
  state.value.count = 2;
  state.value.greet = "Hello, Vue 3";
  state.value.nested.count = 2;
  state.value.list[0].name = "c";
  console.log(JSON.stringify(unref(state))); // 2

  // stater.count = 2;
  // stater.greet = "Hello, Vue 3";
  // stater.nested.count = 2;
  // stater.list[0].name = "c";
  // console.log(JSON.stringify(unref(stater))); // 2
  // 会触发更改
  // state.value = { count: 2 }
  // console.log(state.value.count) // 2
});

function add() {
  state.value.nested.count++;
  console.log(state.value.nested.count);
  stateref.value.nested.count++;
  console.log(stateref.value.nested.count);
  //如果add中直接调用上面代码,内嵌模式不需要写.value
}
</script>

5、总结:

1)单独的去修改shallowRef,shallowReactive变量的值,值是变了,但是视图是不会被响应的,但是如果我们在<script>...</script>中直接改变量的值的时候,是会更改视图显示的,在onMounted中写是不会的,视图不会响应,但值是变了。

2)如果我们在shallowRef,shallowReactive变量的值改变后,再去修改ref,reactive定义的值的时候,视图变成响应式,这样shallowRef,shallowReactive的视图也会一并被更新。

所以如果有些场景考虑到大数据结构的效率的时候,我们要根据这些原理去使用,这样就简单明了了。如果只是console.log是测试不出来问题的,因为一直会改变的。区别只在视图有没有响应。

Vue 3中的`ref`和`reactive`是两个核心的概念,它们在数据绑定和响应式系统中起着重要作用。 1. `ref`: - `ref`是一个特殊的对象,它包装了一个基本类型的值(如字符串、数字或对象)和一个当前值的getter/setter。当你使用`ref`时,Vue会自动跟踪它的值并确保视图更新。 - 它主要用于存储和操作复杂的数据结构,例如对象和数组,因为这些类型不能直接用作模板的绑定目标。 - 示例: ```javascript const count = ref(0); // 创建一个数字类型的引用 ``` - 相关问题: 1. `ref`主要适用于哪种数据类型? 2. 什么时候会在Vue组件中使用`ref`而不是直接绑定变量? 3. 如何使用`ref.value`获取内部存储的原始值? 2. `reactive`: - `reactive`是Vue提供的一个更高阶的功能,用于将整个对象转换为响应式的。当你调用`reactive(obj)`时,Vue会对对象的所有属性进行深度观察,并将其转换为响应式数据。 - 对象内部的变化会自动更新视图,反之亦然。`reactive`通常用于初始化复杂的组件状态,它可以包含嵌套的对象和数组。 - 示例: ```javascript const user = reactive({ name: &#39;Alice&#39;, age: 30 }); ``` - 相关问题: 1. 什么情况下你会选择使用`reactive`而不是单独创建多个`ref`? 2. `reactive`能处理哪些数据类型? 3. `reactive`如何确保视图与数据的一致性? 总结一下,`ref`侧重于管理单个值,而`reactive`用于管理整个可变对象,它们都是Vue3实现数据绑定和响应式设计的关键工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值