1.定义
```
export const state = ()=>({
typeList : []
})
export const mutations = {
SET_TYPE_LIST(state,typeList){
state.typeList = typeList
}
}
export const actions = {
UPDATE_TYPE_LIST({commit,state},typeList){
commit('SET_TYPE_LIST',typeList)
}
}
export const getters = {
getTypeList(state){
return state.typeList
}
}
```
2.使用
```
// 调用actions 异步请求
this.$store.dispatch('UPDATE_TYPE_LIST',[])
// 调用mutations
this.$store.commit('SET_TYPE_LIST',[])
```
辅助函数
```
import {mapActions,mapGetters,mapState,mapMutations} from 'vuex'
methods:{
getTypeList(){
this.UPDATE_TYPE_LIST([])
this.SET_TYPE_LIST([])
},
...mapActions('user',['UPDATE_TYPE_LIST'])
...mapMutations('user',['SET_TYPE_LIST'])
},
// 计算属性
computed:{
..mapGetters('模块名称,根模块可以不传',['getTypeList'])
..mapState('模块名称,根模块可以不传',['typeList '])
}
```