vue3之 shallowRef、markRaw

shallowRef 用于创建一个浅层响应式引用,只对顶层属性进行响应式处理。
markRaw 用于标记一个对象,使其完全跳过 Vue 的响应式系统。

这两者都可以用于优化性能,避免不必要的响应式开销,特别是在处理大型对象或第三方库对象时。

shallowRef

shallowRef 是 Vue 3 中的一个 API,用于创建一个浅层响应式引用。与 ref 不同,shallowRef 只会对其值的顶层进行响应式处理,而不会递归地将其内部的对象变成响应式的。

用法
javascript 复制代码
import { shallowRef } from 'vue';

const state = shallowRef({
  nested: {
    count: 0
  }
});

// 只有 state 自身是响应式的,state.nested 不是响应式的
state.value.nested.count++; // 不会触发响应式更新
适用场景
  • 当你有一个复杂的对象,但只需要对其顶层属性进行响应式处理时。
  • 当你需要避免对大型对象进行深层次的响应式处理以提高性能时。

markRaw

markRaw 是 Vue 3 中的一个 API,用于标记一个对象,使其永远不会被 Vue 的响应式系统处理。被标记为 markRaw 的对象将完全跳过响应式转换。

用法
javascript 复制代码
import { markRaw } from 'vue';

const rawObject = markRaw({
  nested: {
    count: 0
  }
});

// rawObject 及其所有嵌套属性都不是响应式的
rawObject.nested.count++; // 不会触发响应式更新
适用场景
  • 当你有一个对象不需要响应式处理时。
  • 当你需要将第三方库的对象(如 DOM 元素、图表实例等)排除在响应式系统之外时。

下面的例子不能使用 refref 会将其值变成响应式对象,而组件对象不应该是响应式的。为了避免这个问题,可以使用 shallowRef 或者 markRaw 来处理组件对象。

示例:在 Vue 组件中使用 shallowRefmarkRaw

使用 shallowRef
javascript 复制代码
<template>
  <div>
    <button @click="toggleComponent">Toggle Component</button>
    <component :is="currentComponent" />
  </div>
</template>

<script setup>
import { shallowRef } from 'vue';
import ComponentA from './components/ComponentA.vue';
import ComponentB from './components/ComponentB.vue';

const currentComponent = shallowRef(ComponentA);

const toggleComponent = () => {
  currentComponent.value = currentComponent.value === ComponentA ? ComponentB : ComponentA;
};
</script>
使用 markRaw
javascript 复制代码
<template>
  <div>
    <button @click="toggleComponent">Toggle Component</button>
    <component :is="currentComponent" />
  </div>
</template>

<script setup>
import { ref, markRaw } from 'vue';
import ComponentA from './components/ComponentA.vue';
import ComponentB from './components/ComponentB.vue';

const ComponentA_raw = markRaw(ComponentA);
const ComponentB_raw = markRaw(ComponentB);

const currentComponent = ref(ComponentA_raw);

const toggleComponent = () => {
  currentComponent.value = currentComponent.value === ComponentA_raw ? ComponentB_raw : ComponentA_raw;
};
</script>
相关推荐
木斯佳2 分钟前
前端八股文面经大全:腾讯前端一面(2026-04-04)·深度解析
前端·ai·鉴权·monorepo
code_Bo5 分钟前
kiro生成小程序商业案例
前端·微信小程序·小程序·云开发
yellowbuff6 分钟前
为什么你的 0.01 秒倒计时看起来一卡一卡的?
前端
onebyte8bits9 分钟前
NestJS 系列教程(十八):文件上传与对象存储架构(Multer + S3/OSS + 访问控制)
前端·架构·node.js·状态模式·nestjs
Ruihong11 分钟前
放弃 Vue3 传统 <script>!我的 VuReact 编译器做了一次清醒取舍
前端·vue.js
weixin_4561648312 分钟前
vue3 父组件向子组件传参
前端
Beginner x_u15 分钟前
前端八股整理|CSS|高频小题 01
前端·css·八股
蜡台19 分钟前
IDEA LiveTemplates Vue ElementUI
前端·vue.js·elementui·idea·livetemplates
E-cology22 分钟前
【泛微低代码开发平台e-builder】使用HTML组件实现页面中部分区域自定义开发
前端·低代码·泛微·e-builder
用户97514707513623 分钟前
如何使用Promise.any()处理多个异步操作?
前端