Vue 笔记 · 个人理解

computed 和 watch:别混着用

我给自己的分工

computed 回答「根据已有状态算出来是什么」;watch 回答「某值变了之后我要去做点什么」。前者像公式,后者像副作用。

更爱用 computed 的场景

const canSubmit = computed(() => {
  return form.title.trim().length > 0 && !saving.value
})

才会上 watch 的场景

早期我喜欢啥都 watch,结果依赖一变就连环触发,调试很痛苦。现在能 computed 解决的,绝不先写 watch。
Composition API

← 返回 Vue 笔记目录