Vuex 辅助函数实践

Vuex 辅助函数实践

0、初始化

1
2
3
4
5
6
const app = new Vue({
...App,
store,
i18n
})
app.$mount()

1、添加vuex加载器

1
2
import {mapState,mapActions,mapMutations} from "vuex"

2、在computed使用state变量,对应的模块

1
2
3
4
5
computed: mapState({
service_cate: state => state.service.service_cate,
recommend_list: state => state.service.recommend_list,
subColor: state => state.config.configInfo.subColor,
}),

3、在methods中加载对应的方法名,类型【数组】

1
2
3
4
methods: {
...mapActions(['getConfigInfo', 'getUserInfo', 'updateCommonOptions']),
...mapMutations(['updateUserItem', 'updateServiceItem']),
}