signup
signup

Reputation: 165

How can I get stored value in Vue 3?

I want to get the value of my stored user from my store

but when I tried on onMounted function

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)

it does not give the information, I am getting info.name undefined

what happening here?

Upvotes: 2

Views: 1221

Answers (1)

Estus Flask
Estus Flask

Reputation: 222309

info is a ref, it cannot have info.name. It's a mistake to use computed inside mounted hook, it should be directly inside setup:

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

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

store.state.user value is not guaranteed to be up-to-date at the time when it's accessed. In case it changes during the component's lifespan, it needs to be accessed inside a watcher or another computed property.

Upvotes: 4

Related Questions