Skip to content

Effect作用域 API 中等 #Composition API #Reactivity:Advanced

By webfansplz @webfansplz

接受挑战    接受挑战(通过单元测试)    English

在这个挑战中,你将使用 响应式 API: effectScope 来完成它。 以下是你要实现的内容 👇:

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

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

// 使用 `effectScope` API 使这些Effect效果在触发一次后停止

watch(doubled, () => console.log(doubled.value))
watchEffect(() => console.log("Count: ", doubled.value))

counter.value = 2

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

</script>

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


分享你的解答 查看解答