定义响应式数据,
import { reactive} from "vue";
const data= reactive({
name: "孙悟空",
age: 500,
friends: ["猪八戒", "沙和尚"]
});
定义浅响应式数据,只有根数据是响应式
import { shallowReactive} from "vue";
const data= shallowReactive({
name: "孙悟空",
age: 500,
friends: ["猪八戒", "沙和尚"]
});
定义基本类型(简单对象)响应式数据,需要使用.value操作数据
import { ref } from "vue";
const count = ref(0)
function clickHandler(){
count.value++
}
定义计算属性
import { computed } from 'vue'
const content = ref('测试内容');
const getLen = computed(() => {
console.log("计算属性执行了")
return content.value.length
})
监视属性
import { watch} from 'vue'
cosnt count = ref(0)
watch(count,(newValue,oldValue) => {
console.log(newValue,oldValue)
})
const data= reactive({
name: "孙悟空",
age: 500,
friends: ["猪八戒", "沙和尚"]
});
//监视age
watch(() => data.age,(newValue,oldValue) => {
console.log(newValue,oldValue)
})
副作用函数,如果函数内部用到的响应式数据变动,函数体就会自动执行
import {ref,watchEffect} from 'vue'
const count = ref(0)
function clickHandler(){
count.value++
}
watchEffect(() => {
console.log("副作用函数执行了")
console.log('count: '+count.value)
})