vue3 可以写多个watch
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; }), radioIndex: computed(() => {
return store.state.headerObj.radioIndex }), }) //watch监听动态拿到值的变化,从而做出反应 watch(()
=> state.radioVal,(newVal,oldVal)=>{ console.log(newVal) console.log(oldVal) }
//如果想监听多个值,在多写几个watch watch(()=> state.radioIndex,(newVal,oldVal)=>{ console.log
(newVal,oldVal,'测试') }) return{ ...toRefs(state), } }, }