Skip to content

In nuxt3, how does pinia get a store by ID #2129

Closed Answered by Ilanaya
gllinzbk1 asked this question in Help and Questions
Discussion options

You must be logged in to vote

@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 like

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:
Use pinia plugin API to place registered stores in your store.

const pinia = createPinia();
pinia.use(({ store }) => {
  const globalStore = useGlobalStore();

  if (!globalStore.allPiniaStores.has(store.$id)) {
    globalStore.allPiniaStores.set(store.$id, store);
  }

Replies: 1 comment 1 reply

Comment options

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

Answer selected by gllinzbk1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants