由于没有仔细阅读vue3.0的官方文档,在给ref()变量赋值的时候遇到了问题
众所周知,vue3.0增加了ref()、reactive()语法糖。
- ref() 基础类型、数组
- reactive() 对象
两者除了包裹的内容不同,在使用方法上也有一定的区别。
<template>
<div>{{ refData }}</div>
<div>{{ reactiveData.data }}</div>
</template>
setup() {
const refData = ref('')
const reactiveData = reactive({
data: ''
})
onMounted(() => {
refData.value = 'refData赋值'
reactiveData.data = 'reactiveData赋值'
})
return {
refData, reactiveData
}
}
通过上面的代码我们可以看出,在模板中,ref()和reactive()的数据直接引用即可;但是在setup中,ref()数据需要给其value赋值,并且使用时也需要refData.value,而reactive()的数据直接引用。