定义响应式数据
- ref(可以定义引用类型和基础类型变量)
const data= ref({key:value})
const data= ref(0)
还可以获取定义了ref属性的dom
const refName= ref(null) //refName必须与定义的ref属性一致 - reactive(只能定义引用类型变量)
const data= reactive({key:value})
计算器属性
const newAttr= computed(() => {
return oldAttr.value.filter((item) => item > 2)
})
侦听器
- 监听单个属性
const state = reactive({ count: 0 })
watch(
() => state.count,
(count, prevCount) => {
/* ... */
},
{deep:true},//深度监听
{immediate:true} //立即监听
) - 监听多个属性
watch(
[attr1,attr2],
([attr1New,attr1Old],[attr2New,attr2Old]) => {
/* ... */
})