get store by id #753
-
Hi devs, Maybe someone can answer - is there a possibility to access the "pinia" store by id. Thank you in advance TJ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
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 |
Beta Was this translation helpful? Give feedback.
-
Eduardo thanks for the explanation. |
Beta Was this translation helpful? Give feedback.
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:
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…