Skip to content

响应式工具

响应式工具用于处理只读、浅响应、原始对象和 effect 作用域。

readonly

ts
const state = readonly(useReactive({ count: 0 }));

只读对象被写入时会在开发环境给出警告。

shallow

ts
const value = useShallowRef({ nested: { count: 0 } });
const state = useShallowReactive({ nested: { count: 0 } });

浅响应只追踪顶层,适合大型对象或第三方实例包装。

markRaw / toRaw

ts
const editor = markRaw(createEditor());

WARNING

markRaw() 标记对象不要被代理,toRaw() 取回原始对象。

effectScope

ts
const scope = effectScope();

scope.run(() => {
  useEffect(() => {
    // ...
  });
});

scope.stop();

作用域适合在插件、弹层、临时模块里批量管理 effect。