vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

8 - effectScope API

Open zhang13pro opened this issue 2 years ago • 0 comments

// your answers
<script setup lang="ts">
import { ref, computed, watch, watchEffect, effectScope } from "vue"

const scope = effectScope()
const counter = ref(1)
const doubled = computed(() => counter.value * 2)

// use the `effectScope` API to make these effects stop together after being triggered once

scope.run(() => {
  watch(doubled, () => console.log(doubled.value))
	watchEffect(() => console.log(`Count: ${doubled.value}`))
})


counter.value = 2

setTimeout(() => {
  scope.stop() 
  counter.value = 4
})

</script>

<template>
  <div>
    <p>
      {{ doubled }}
    </p>
  </div>
</template>

zhang13pro avatar Sep 21 '22 02:09 zhang13pro