Skip to content

get store by id #753

Closed Answered by posva
tyrunasj asked this question in Help and Questions
Oct 28, 2021 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

You can't do it (at least with public API) because by design you should import every store you use so code splitting works correctly. In order to make what you want work, you would have to keep track of installed stores. This is actually very simple to setup with a plugin:

import type { StoreGeneric } from 'pinia'

const storeMap = new Map<string, StoreGeneric>()
pinia.use(({ store }) => {
  storeMap.set(store.$id, store)
})

Then you can call storeMap.get(id) but it won't be able to instantiate the store because it's not aware of all the imports. You could create another function that dynamically imports the store based on its filename if it hasn't been used yet but that would make it asy…

Replies: 2 comments 1 reply

Comment options

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

Answer selected by tyrunasj
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants