2

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?

Estus Flask
  • 179,509
  • 61
  • 360
  • 499
signup
  • 117
  • 9

1 Answers1

4

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.

Estus Flask
  • 179,509
  • 61
  • 360
  • 499