Skip to content

5 - watch 全家桶 #2255

Open
Open
@mu-muw

Description

@mu-muw
// 你的答案
<template>
  <div>
    <p>
      {{ count }}
    </p>
    <p ref="eleRef">
      {{ age }}
    </p>
  </div>
</template>
<script setup >
import { ref, watch } from "vue"
const count = ref(0)
/**
 * 挑战 1: Watch 一次
 * 确保副作用函数只执行一次
*/
const unWatch = watch(count, () => {
  console.log("Only triggered once")
  unWatch()
})
count.value = 1
setTimeout(() => count.value = 2)
/**
 * 挑战 2: Watch 对象
 * 确保副作用函数被正确触发
*/
const state = ref({
  count: 0,
})
watch(state, () => {
  console.log("The state.count updated")
}, {
  deep: true
})
state.value.count = 2
/**
 * 挑战 3: 副作用函数刷新时机
 * 确保正确访问到更新后的`eleRef`值
*/
const eleRef = ref()
const age = ref(2)
watch(age, () => {
  console.log(eleRef.value)
}, {
  flush: 'post'
})
age.value = 18 
</script> 


Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions