Skip to content

setup组合式api,在promise里注册的watch是属于异步注册吗? #8266

Discussion options

You must be logged in to vote

异步注册的话你只要在onUnmounted的时候stop watchEffect即可

<script setup>
import { watchEffect, onUnmounted } from 'vue'

let stop = null

setTimeout (() => {
 stop = watchEffect(() => {})
})

onUnmounted(() => {
  stop()
})
</script>

由于不是很清楚你的表单业务情况,但是我感觉这些你可以完全提早watch构建好,因为你首先肯定要有一个响应式对象去存储这个表单数据才能使用watch,如果是不想第一次赋值触发watch内的处理,你可以做的flag判断位即可。或者是你script 通过异步执行,给你的请求加上await,使你的请求变成同步执行, 但是意味着你的组件需要通过Suspense组件包裹使用。

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@xhc-code
Comment options

@chenxch
Comment options

@xhc-code
Comment options

Answer selected by xhc-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants