怎么解决 Getting a value from the props in root scope of script setup will cause the value to lose reactivity
您可以将 props 值存储在 ref 中,以保持其响应性。例如:
<template>
<div>{{ myProp }}</div>
</template>
<script setup>
import { ref } from 'vue'
const myPropRef = ref(myProp)
// 在需要时更新 myPropRef
watch(() => myProp, (newVal) => {
myPropRef.value = newVal
})
const myProp = myPropRef.value
</script>
这样,您就可以在 <template> 中使用 myProp,同时仍然具有响应性。
原文地址: https://www.cveoy.top/t/topic/JH9 著作权归作者所有。请勿转载和采集!