• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • effectScope()

    创建一个effect作用域对象,以捕获在其内部创建的响应式effect(例如计算属性或侦听器),使得这些effect可以一起被处理。

    类型

    function effectScope(detached?: boolean): EffectScope
    
    interface EffectScope {
      run<T>(fn: () => T): T | undefined // undefined if scope is inactive
      stop(): void
    }
    

    例子

    const scope = effectScope()
    
    scope.run(() => {
      const doubled = computed(() => counter.value * 2)
    
      watch(doubled, () => console.log(doubled.value))
    
      watchEffect(() => console.log('Count: ', doubled.value))
    })
    
    // // 处理该作用域内的所有 effect
    scope.stop()
    

    上篇:markRaw()

    下篇:getCurrentScope()