我想从我的商店获取我存储的用户的值(value)
但是当我尝试 onMounted 函数时
onMounted(async () => {
console.log('user', store.state.user) //here i can see the values
const info = computed(() => {
return store.state.user
})
console.log('info', info)
它没有提供信息,我得到info.name
未定义
这里发生了什么?
最佳答案
info
是一个引用,它不能有 info.name
。在 mounted
钩子(Hook)中使用 compulated
是错误的,它应该直接在 setup
中:
const info = computed(() => store.state.user)
onMounted(async () => {
console.log('info', info.value)
});
store.state.user
值在访问时不保证是最新的。如果它在组件的生命周期内发生变化,则需要在观察程序或另一个计算属性内访问它。
关于javascript - 如何在 Vue 3 中获取储值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70843486/