import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'
import getters from './getters'
import a from './module/a'
Vue.use(Vuex)
export default new Vuex.Store({
state,
mutations,
actions: {
},
getters,
modules:{
a,
}
})
仓库里分其他模块module,模块中也包含state,mutation,action,getters这四个部分
模块中的mutation,action,getters是注册在全局vuex中,只有模块中的state是私有的
state访问代码会繁琐一点 $this.$store.state.moduleName.Name,module模块是放在vue state中的,可以通过vue-tool看到
其他三个部分跟平时使用的方法一样,mapGetters放到computer中;mapMutations,mapActions放到methods中映射
<script>
import comheader from "../../components/Header";
import {mapGetters,mapMutations,mapActions} from 'vuex'
export default {
name:'search',
components: { comheader },
computed:{
...mapGetters(['ModolueGetterName'])
},
methods: {
...mapMutations(['ModolieMutationName']),
...mapActions(['ModolieActionName'])
},
}
</script>
如果你想要模块modules中私有化的话,需要将在modules 中增加
namespaced: true,
那在组件中映射map这样写
<script>
import comheader from "../../components/Header";
import {mapGetters,mapMutations,mapActions} from 'vuex'
export default {
name:'search',
components: { comheader },
computed:{
...mapGetters('a',['ModolueGetterName'])
},
methods: {
...mapMutations('a',['ModolieMutationName']),
...mapActions('a',['ModolieActionName'])
},
}
</script>
//a 为模块名