• 首页
  • css3教程
  • html5教程
  • jQuery手册
  • vue手册
  • php手册
  • MySQL手册
  • apache手册
  • redis手册
  • watchEffect()

    在响应式地跟踪其依赖项时立即运行一个函数,并在更改依赖项时重新运行它。

    const count = ref(0)
    
    watchEffect(() => console.log(count.value))
    // -> logs 0
    
    setTimeout(() => {
    	count.value++
    	// -> logs 1
    	}, 100)
    

    类型声明:

    function watchEffect(
    	effect: (onInvalidate: InvalidateCbRegistrator) => void,
    	options?: WatchEffectOptions
    ): StopHandle
    
    interface WatchEffectOptions {
    	flush?: 'pre' | 'post' | 'sync'  // default: 'pre'
    	onTrack?: (event: DebuggerEvent) => void
    	onTrigger?: (event: DebuggerEvent) => void
    }
    
    interface DebuggerEvent {
    	effect: ReactiveEffect
    	target: any
    	type: OperationTypes
    	key: string | symbol | undefined
    }
    
    type InvalidateCbRegistrator = (invalidate: () => void) => void
    
    type StopHandle = () => void
    

    上篇:reactive()

    下篇:watch()