Skip to content

use onMounted is a pinia store work well #1508

Closed Answered by posva
allenchannn asked this question in Help and Questions
Discussion options

You must be logged in to vote

You should usually avoid it as the store could be instantiated outside of a component lifecycle and therefore never trigger the function passed to onMounted().

You should also guard it:

import { getCurrentInstance } from 'vue'

// ...
if (getCurrentInstance()) {
  onMounted(() => {
  })
}

An equivalent to onUnmounted() for stores is onScopeDispose() (https://vuejs.org/api/reactivity-advanced.html#onscopedispose) but this only triggers if you dispose of the store with store.$dispose()

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by posva
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@posva
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1506 on August 01, 2022 13:00.