In nuxt3, how does pinia get a store by ID #2129
-
Describe the feature
//-------------------------use layer1
//----------------------get,maybe,i have many layers,just first layer set data,others layer just get store by id The purpose is to store different categories of data in different stores |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@gllinzbk1 See the last two commits in this repo: https://github.com/maIIady/pinia-tests Possible solution 1: make a simple function that accesses the internal const getStoreById = (id: string) => {
//@ts-expect-error _s is internal
return getActivePinia()?._s.get(id);
}; This should work, but I don't think there is a way to get proper typing for this implementation (see mentioned repo) Solution 2: const pinia = createPinia();
pinia.use(({ store }) => {
const globalStore = useGlobalStore();
if (!globalStore.allPiniaStores.has(store.$id)) {
globalStore.allPiniaStores.set(store.$id, store);
}
const { $dispose } = store;
store.$dispose = () => {
$dispose();
globalStore.allPiniaStores.delete(store.$id);
};
}); This is a better solution because you do not rely on the pinia's internals, however, typing is problematic here too. You can see the last commit in the repo to check the actual code. |
Beta Was this translation helpful? Give feedback.
@gllinzbk1 See the last two commits in this repo: https://github.com/maIIady/pinia-tests
Possible solution 1: make a simple function that accesses the internal
_s
property. Smth likeThis should work, but I don't think there is a way to get proper typing for this implementation (see mentioned repo)
Solution 2:
Use pinia plugin API to place registered stores in your store.