主题
watchIgnorable
可忽略的监视
🌐 Ignorable watch
示例
用法
🌐 Usage
扩展的 watch 返回额外的 ignoreUpdates(updater) 和 ignorePrevAsyncUpdates(),以忽略对源的特定更新。
🌐 Extended watch that returns extra ignoreUpdates(updater) and ignorePrevAsyncUpdates() to ignore particular updates to the source.
ts
import { watchIgnorable } from '@vueuse/core'
import { nextTick, shallowRef } from 'vue'
const source = shallowRef('foo')
const { stop, ignoreUpdates } = watchIgnorable(
source,
v => console.log(`Changed to ${v}!`),
)
source.value = 'bar'
await nextTick() // logs: Changed to bar!
ignoreUpdates(() => {
source.value = 'foobar'
})
await nextTick() // (nothing happened)
source.value = 'hello'
await nextTick() // logs: Changed to hello!
ignoreUpdates(() => {
source.value = 'ignored'
})
source.value = 'logged'
await nextTick() // logs: Changed to logged!WatchOptionFlush 时间
🌐 WatchOptionFlush timing
watchIgnorable 接受与 watch 相同的选项,并使用相同的默认值。因此,默认情况下,该组合式使用 flush: 'pre'。
ignorePrevAsyncUpdates
此功能仅适用于异步刷新 'pre' 和 'post'。如果使用 flush: 'sync',ignorePrevAsyncUpdates() 将不会起作用,因为观察者会在每次源更新后立即触发。它仍然为同步刷新提供,以便代码可以更加通用。
🌐 This feature is only for async flush 'pre' and 'post'. If flush: 'sync' is used, ignorePrevAsyncUpdates() is a no-op as the watch will trigger immediately after each update to the source. It is still provided for sync flush so the code can be more generic.
ts
import { watchIgnorable } from '@vueuse/core'
import { nextTick, shallowRef } from 'vue'
const source = shallowRef('foo')
const { ignorePrevAsyncUpdates } = watchIgnorable(
source,
v => console.log(`Changed to ${v}!`),
)
source.value = 'bar'
await nextTick() // logs: Changed to bar!
source.value = 'good'
source.value = 'by'
ignorePrevAsyncUpdates()
await nextTick() // (nothing happened)
source.value = 'prev'
ignorePrevAsyncUpdates()
source.value = 'after'
await nextTick() // logs: Changed to after!推荐读物
🌐 Recommended Readings
- 可忽略监视 - 由 @patak-dev 制作