我有一个使用 v3 组合 API 的 Vue 组件:
<template>
<input type="checkbox" v-model="playing" id="playing" @input="$emit('play', $event.target.value)" />
<label for="playing" >{{ playing ? 'Pause' : 'Play' }}</label>
</template>
<script>
export default {
props: {
done: Boolean
},
setup(props) {
const playing = ref(true)
watchEvent(() => {
if (props.done) {
playing.value = false
this.$emit('play', playing.value)
}
}
return { playing }
}
}
</script>
当 watchEvent 运行时,我收到错误消息Cannot read property "$emit" of undefined
。看起来我没有使用错误类型的函数(箭头与正常函数)。
无论是函数还是箭头函数,它似乎this
始终是未定义的。setup()