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>
相关推荐
Guheyunyi4 分钟前
智能照明系统:照亮智慧生活的多重价值
大数据·前端·人工智能·物联网·信息可视化·生活
徐同保19 分钟前
通过AzureOpenAI请求gpt-4.1-mini
前端
红尘散仙30 分钟前
四、WebGPU 基础入门——Uniform 缓冲区与内存对齐
前端·rust·gpu
进取星辰41 分钟前
13、性能优化:魔法的流畅之道——React 19 memo/lazy
前端·react.js·前端框架
zwjapple1 小时前
React中createPortal 的详细用法
前端·javascript·react.js
小矮马1 小时前
React-组件通信
前端·javascript·react.js
codingandsleeping1 小时前
pnpm + monorepo:高效的项目管理方式
前端
程序员三千_1 小时前
最近爆火的MCP到底是什么?
前端
古时的风筝1 小时前
暴论:2025年,程序员必学技能就是MCP
前端·后端·mcp
古时的风筝1 小时前
这编程圈子变化太快了,谁能告诉我 MCP 是什么
前端·后端·mcp