Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

Importing vuex plugins from the module files #693

@JulianGaibler

Description

@JulianGaibler

I was desperately trying to make Plugins work for a module of mine, so I came up with some minor code changes that can make that happen.

The most significant change here is that I set namespaced to true. Also, the Plugins have to check with mutation.type if they are responsible because they are subscribed to every mutation, not only the one of their module. But except that it works pretty nice for me.

I'm not sure if this is a good addition to the project-scaffolding, so let me know what you think of this solution!

  • renderer/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

import { modules, plugins } from './modules'

Vue.use(Vuex)

export default new Vuex.Store({
	modules,
	plugins,
	strict: process.env.NODE_ENV !== 'production'
})
  • renderer/store/modules/index.js
const files = require.context('.', false, /\.js$/)
const modules = {}
const plugins = []

files.keys().forEach(key => {
	if (key === './index.js') return;
	modules[key.replace(/(\.\/|\.js)/g, '')] = files(key)._modules;
	plugins.push(...files(key)._plugins);
})

export { modules, plugins }
  • renderer/store/modules/anyModule.js
...

const _modules = {
	namespaced: true,
	state,
	mutations,
	actions,
}
const _plugins = [ plugin1, plugin2 ]

export { _modules, _plugins }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions