Skip to content

8 - Effect作用域 API #2961

Open
Open
@XiaoPingzi-Anhui

Description

@XiaoPingzi-Anhui
// 你的答案
<script setup lang="ts"> import { ref, computed, watch, watchEffect ,effectScope} from "vue" const counter = ref(1) const doubled = computed(() => counter.value * 2) // use the `effectScope` API to make these effects stop together after being triggered once const scope=effectScope() scope.run(()=>{ watch(doubled, () => { scope.stop(); console.log(doubled.value) }) watchEffect(() => { console.log(`Count: ${doubled.value}`) }) }) counter.value = 2 setTimeout(() => { counter.value = 4 }) </script>

{{ doubled }}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions