Skip to content

Accessing other stores within a store with a stores option #625

Open
@b-strauss

Description

@b-strauss

What problem is this solving

We are currently using Vuex and are evaluating switching to Pinia. We have 10+ stores with different responsibility. Many of these stores use other stores as dependency in a lot of places. There is one store that calls the getters of another store in almost every one of it's own getters. As far as I understand it, with pinia you have to call the createStore function in every getter where you want to use the other store, otherwise the store initialization would not work. This would result in writing the same line of code over and over again in all places (example: const user = useUserStore();).

Proposed solution

I propose an additional option to define which stores you want to use in the current store. This would also have the benefit that it would nicely list all the store dependencies in one place.

export const useCartStore = defineStore('cart', {
  stores: [useUserStore, useDataStore],
  getters: {
    myData() {
      return {
        name: this.userStore.fullname,
        items: this.dataStore.itemsForUser(this.userStore.id),
      };
    },
  },
});

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    💬 In discussion

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions