Skip to content

feat: Allow plugins in modules #1905

Open
@hackel

Description

@hackel

What problem does this feature solve?

Currently plugins are only loaded in the root store instance and are silently ignored in modules. It would be convenient and consistent to be able to register plugins within a module. The plugin should receive a store with module-scoped state, getters, rootState, etc. just like an action. This would allow for better separation of concerns and avoid having to modify the root store for things that are specific to a single module.

What does the proposed API look like?

Contrived example:

const store = new Vuex.Store({
  modules: {
    Module: {
      namespaced: true,
      state: { count: 0, countPlusOne: 1 },
      mutations: {
        setCountPlusOne: (state, count) => (state.countPlusOne = count),
      },
      plugins: [
        store => {
          store.watch(
            (state, getters) => state.count,
            (newVal, oldVal) => store.commit('setCountPlusOne', newVal)
          );
        },
      ],
    },
  },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions