Skip to content

Commit 709b46d

Browse files
committed
add namespace presence check in helpers
1 parent 81c35d4 commit 709b46d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/helpers.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ export const mapState = normalizeNamespace((namespace, states) => {
55
let state = this.$store.state
66
let getters = this.$store.getters
77
if (namespace) {
8-
const module = this.$store._modulesNamespaceMap[namespace]
8+
const module = getModuleByNamespace(this.$store, 'mapState', namespace)
99
if (!module) {
10-
warnNamespace('mapState', namespace)
1110
return
1211
}
1312
state = module.context.state
@@ -26,6 +25,9 @@ export const mapMutations = normalizeNamespace((namespace, mutations) => {
2625
normalizeMap(mutations).forEach(({ key, val }) => {
2726
val = namespace + val
2827
res[key] = function mappedMutation (...args) {
28+
if (namespace && !getModuleByNamespace(this.$store, 'mapMutations', namespace)) {
29+
return
30+
}
2931
return this.$store.commit.apply(this.$store, [val].concat(args))
3032
}
3133
})
@@ -37,8 +39,12 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
3739
normalizeMap(getters).forEach(({ key, val }) => {
3840
val = namespace + val
3941
res[key] = function mappedGetter () {
42+
if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
43+
return
44+
}
4045
if (!(val in this.$store.getters)) {
4146
console.error(`[vuex] unknown getter: ${val}`)
47+
return
4248
}
4349
return this.$store.getters[val]
4450
}
@@ -51,6 +57,9 @@ export const mapActions = normalizeNamespace((namespace, actions) => {
5157
normalizeMap(actions).forEach(({ key, val }) => {
5258
val = namespace + val
5359
res[key] = function mappedAction (...args) {
60+
if (namespace && !getModuleByNamespace(this.$store, 'mapActions', namespace)) {
61+
return
62+
}
5463
return this.$store.dispatch.apply(this.$store, [val].concat(args))
5564
}
5665
})
@@ -75,6 +84,10 @@ function normalizeNamespace (fn) {
7584
}
7685
}
7786

78-
function warnNamespace (helper, namespace) {
79-
console.error(`[vuex] module namespace not found in ${helper}(): ${namespace}`)
87+
function getModuleByNamespace (store, helper, namespace) {
88+
const module = store._modulesNamespaceMap[namespace]
89+
if (!module) {
90+
console.error(`[vuex] module namespace not found in ${helper}(): ${namespace}`)
91+
}
92+
return module
8093
}

0 commit comments

Comments
 (0)