import { reactive,toRefs,computed,watch } from 'vue';
import { useStore } from 'vuex';
export default {
setup(){
//定义vuex方法
const store = useStore();
//初始化 相当于vue2的data
const state = reactive({
//使用计算属性动态拿到vuex的值
radioVal: computed(() => {
return store.state.headerObj.radioVal;
}),
})
//watch监听动态拿到值的变化,从而做出反应
watch(()=> state.radioVal,(newVal,oldVal)=>{
console.log(newVal)
console.log(oldVal)
}
//如果想监听多个值,在多写几个watch
return{
...toRefs(state),
}
},
}
vue3使用computed和watch来监听数据变化
最新推荐文章于 2024-07-25 11:05:20 发布