vue3中ref和reactive定义响应式数据的区别

ref:ref用来将原始数据类型定义为响应式数据,例如

setup(){
const count = ref(0)//0是初始值
const add = ()=>{
count.value++
}
return {count,add}
}

reactive用于将复杂数据定义为响应式数据(例如对象)

const obj = reactive({
name:'1s',
age:10,
brand:{
id:1,
name:'宝马'
}
})
const updateName = ()=>{
obj.name = 'zs'
}
const updateBrandName = ()=>{
obj.brand.name = '奔驰'
}
return {obj,updateName ,updateBrandName }

你可能感兴趣的:(vue.js,前端,javascript)