vuex

181 阅读1分钟

利用...mapMutations的方法,使用拓展运算符把mutations的方法解构在methods中 在this中也可以找到对应解构出来的方法

如果mapMutations中的方法名和methods中的同名了,可以采用对象的形式修改

import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'

methods:{

changeStr(){ this.$store.commit('modA/CHAGESTR','啦啦啦') },

ajaxchange(){ this.$store.dispatch('modA/ajaxStr','哇哇哇') },

computed:{ ...mapState(['num']),

...mapState({ list:(state)=>state.list/* 这也是个改名字的方法 */ }),

...mapGetters(['getthree']),

用了vuex的辅助函数,相当于把值都放在vue实例化对象里面了,所以可以this.出来

有一种场景 页面有一个和getters属性相同的名字 你想要给getters喊一个名字 可以使用对象的名字

...mapGetters({ three:'getthree'}),

这里需要注意,把getters里面的属性写在后面value中,把需要改的名字写在key上