vuex getters 重名问题
本文介绍了vuex getters 重名问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
vuex2.X版本新增了modules模块的概念,当页面越来越多时,每个模块的getters和actions肯定会出现同名的情况,而且由于 getters 不区分模块,所以不同模块中的 getters 如果重名,Vuex 会报出 'duplicate getter key: [重复的getter名]' 错误,actions重名会导致每个actions都会去调用一遍,这个在设计上是否有缺陷?当初尤大大为何会这样设计?目前来看,如何优雅的解决重名问题?
解决方案
用命名空间解决:https://vuex.vuejs.org/en/mod...
const store = new Vuex.Store({
modules: {
account: {
namespaced: true,
// module assets
state: { ... }, // module state is already nested and not affected by namespace option
getters: {
isAdmin () { ... } // -> getters['account/isAdmin']
},
actions: {
login () { ... } // -> dispatch('account/login')
},
mutations: {
login () { ... } // -> commit('account/login')
}
}
}
})
这篇关于vuex getters 重名问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文