如何在 Vue 3 中获取储值?

How can I get stored value in Vue 3?

我想从我的商店中获取我存储的用户的价值

但是当我尝试 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 undefined

这里发生了什么?

info 是一个 ref,它不能有 info.name。在mounted hook里面使用computed是错误的,应该直接在setup:

里面
const info = computed(() => store.state.user)

onMounted(async () => {
      console.log('info', info.value)
});

store.state.user 值在访问时不保证为 up-to-date。如果它在组件的生命周期内发生变化,则需要在观察者或另一个计算器中访问它 属性.