diff --git a/src/module/module-collection.js b/src/module/module-collection.js index a68f05a51..16febe4a1 100644 --- a/src/module/module-collection.js +++ b/src/module/module-collection.js @@ -53,6 +53,14 @@ export default class ModuleCollection { parent.removeChild(key) } + + isRegistered (path) { + const parent = this.get(path.slice(0, -1)) + const key = path[path.length - 1] + + return parent.hasChild(key) + } + } function update (path, targetModule, newModule) { diff --git a/src/module/module.js b/src/module/module.js index e856333d0..6256af96f 100644 --- a/src/module/module.js +++ b/src/module/module.js @@ -30,6 +30,10 @@ export default class Module { return this._children[key] } + hasChild (key) { + return key in this._children + } + update (rawModule) { this._rawModule.namespaced = rawModule.namespaced if (rawModule.actions) { diff --git a/src/store.js b/src/store.js index 55edd9ea9..544a5175b 100644 --- a/src/store.js +++ b/src/store.js @@ -185,6 +185,16 @@ export class Store { resetStore(this) } + hasModule (path) { + if (typeof path === 'string') path = [path] + + if (process.env.NODE_ENV !== 'production') { + assert(Array.isArray(path), `module path must be a string or an Array.`) + } + + return this._modules.isRegistered(path) + } + hotUpdate (newOptions) { this._modules.update(newOptions) resetStore(this, true) diff --git a/test/unit/modules.spec.js b/test/unit/modules.spec.js index bc8fa2247..a57e3d1c7 100644 --- a/test/unit/modules.spec.js +++ b/test/unit/modules.spec.js @@ -80,6 +80,17 @@ describe('Modules', () => { store.commit('a/foo') expect(mutationSpy).toHaveBeenCalled() }) + it('dynamic module existance test', () => { + const store = new Vuex.Store({ + }) + + store.registerModule('bonjour', { + }) + + expect(store.hasModule('bonjour')).toBe(true) + store.unregisterModule('bonjour') + expect(store.hasModule('bonjour')).toBe(false) + }) it('dynamic module registration preserving hydration', () => { const store = new Vuex.Store({})