Skip to content

8 - effectScope API #1190

Open
Open
@songwuk

Description

@songwuk
<script setup lang="ts">
import { ref, computed, watch, watchEffect,effectScope } from "vue"

const counter = ref(1)
const doubled = computed(() => counter.value * 2)
const scope = effectScope()
// 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(() => {
  counter.value = 4
  scope.stop()
})

</script>

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions