主题
watchPausable
可暂停监视
🌐 Pausable watch
INFO
此函数将在未来版本中被移除。
TIP
自 Vue 3.5 起,已添加 Pausable Watcher,请改用 const { stop, pause, resume } = watch(watchSource, callback)。
示例
用法
🌐 Usage
像平常一样使用 watch,但返回额外的 pause() 和 resume() 函数以进行控制。
🌐 Use as normal the watch, but return extra pause() and resume() functions to control.
ts
import { watchPausable } from '@vueuse/core'
import { nextTick, shallowRef } from 'vue'
const source = shallowRef('foo')
const { stop, pause, resume } = watchPausable(
source,
v => console.log(`Changed to ${v}!`),
)
source.value = 'bar'
await nextTick() // Changed to bar!
pause()
source.value = 'foobar'
await nextTick() // (nothing happend)
resume()
source.value = 'hello'
await nextTick() // Changed to hello!