Skip to content

Reactivity API(响应式与调度)

本页覆盖 wevu 响应式层的全部公开函数,包括“业务常用”和“调试/底层”两类。

1. 状态创建与派生

API类型入口说明
refRef创建基础响应式引用。
reactiveobject创建深层响应式对象。
shallowRefShallowRef只追踪 .value 变更。
shallowReactiveobject仅顶层属性响应式。
readonlyReadonly<T>只读代理。
computedComputedRef / WritableComputedRef声明计算属性(只读/可写)。

2. 监听与副作用

API类型入口说明
watchWatchOptions / WatchStopHandle侦听 source 变化。
watchEffectWatchStopHandle自动收集依赖并执行副作用。
effectWatchStopHandle底层副作用 API。
effectScopeEffectScope作用域级管理 effect。
getCurrentScopeEffectScope获取当前 effect scope。
onScopeDispose() => voidscope 销毁时回调。
stopvoid停止某个 effect/watch。

3. Ref/Proxy 工具

API类型入口说明
toRefRef把对象属性转换为 ref。
toRefsToRefs批量转换对象属性为 ref。
unrefT取 ref.value 或原值。
toValueMaybeRefOrGetter统一展开值/Ref/getter。
triggerRefvoid手动触发 shallowRef 更新。
toRawT获取原始对象。
markRawT标记对象跳过代理。

4. 响应式判定

API类型入口说明
isReftype predicate判定是否 Ref。
isReactivetype predicate判定是否 reactive 代理。
isShallowReftype predicate判定是否 shallowRef。
isShallowReactivetype predicate判定是否 shallowReactive。
isRawboolean判定对象是否被 markRaw

5. 批处理与调度

API类型入口说明
nextTickPromise<void>等待当前微任务批次完成。
batch() => void在单次批处理中执行函数。
startBatchvoid手动开始批处理。
endBatchvoid手动结束批处理。

6. 调试与底层能力

这组 API 通常用于框架层、性能调试或复杂序列化场景:

API类型入口说明
prelinkReactiveTreePrelinkReactiveTreeOptions预链接对象树,优化后续路径追踪。
touchReactivevoid手动触发 reactive 树探测。
traversevoid深度遍历依赖收集辅助。
getReactiveVersionnumber读取响应式内部版本号。
getDeepWatchStrategystring获取当前深度 watch 策略。
setDeepWatchStrategyvoid修改深度 watch 策略(测试/调优)。

7. 组合示例(script setup)

vue
<script setup lang="ts">
import { computed, nextTick, reactive, toRefs, watch, watchEffect } from 'wevu'

// [TS-only] 此示例无专属语法,TS/JS 写法一致。
const state = reactive({ count: 0, unit: 2 })
const { count } = toRefs(state)
const doubled = computed(() => count.value * state.unit)

watch(count, (n) => {
  console.log('count changed:', n)
})

watchEffect(() => {
  console.log('preview:', doubled.value)
})

async function inc() {
  count.value += 1
  await nextTick()
}
</script>

<template>
  <button @tap="inc">
    count: {{ count }} / doubled: {{ doubled }}
  </button>
</template>
vue
<script setup>
import { computed, nextTick, reactive, toRefs, watch, watchEffect } from 'wevu'

const state = reactive({ count: 0, unit: 2 })
const { count } = toRefs(state)
const doubled = computed(() => count.value * state.unit)

watch(count, (n) => {
  console.log('count changed:', n)
})

watchEffect(() => {
  console.log('preview:', doubled.value)
})

async function inc() {
  count.value += 1
  await nextTick()
}
</script>

<template>
  <button @tap="inc">
    count: {{ count }} / doubled: {{ doubled }}
  </button>
</template>

Released under the MIT License.