在vuex中改变了state里默认数据 但是页面没有改变 还是默认数据
let store=useStore()
let info=computed(()=>store.state.info)
return {...toRefs(info)}
在vuex中改变了state里默认数据 但是页面没有改变 还是默认数据
let store=useStore()
let info=computed(()=>store.state.info)
return {...toRefs(info)}
这可能是因为在vue3中,setup函数里面从vuex中弄过来的数据是一次性的,也就是说它不会自动监听vuex中state的变化。可以在setup函数里面使用watch来监听vuex中state的变化,并在变化时重新赋值给info,这样页面就会更新。
示例代码:
let store=useStore()
let info=ref(store.state.info)
watch(() => store.state.info, (newValue) => {
info.value = newValue
})