State
通过this.$store.state.count获取vuex里的值
import{mapState}from'vuex'
computed:{localComputed(){/* ... */},// 使用对象展开运算符将此对象混入到外部对象中...mapState([count])}//count混入到data{}里了
getters
getters:{doneTodos:state=>{returnstate.todos.filter(todo=>todo.done)}}
this.$store.getters.doneTodosCoun获取到getters里的方法
import{mapGetters}from'vuex'
computed:{// 使用对象展开运算符将 getter 混入 computed 对象中...mapGetters(['doneTodosCount','anotherGetter',// ...])}
mutations
mutations:{increment(state){// 变更状态state.count++}}
store.commit('increment')
import{mapMutations}from'vuex'
methods:{...mapMutations(['increment',// 将 `this.increment()` 映射为 `this.$store.commit('increment')`// `mapMutations` 也支持载荷:'incrementBy'// 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`]),...mapMutations({add:'increment'// 将 `this.add()` 映射为 `this.$store.commit('increment')`})}
actions:{increment(context){context.commit('increment')}}
store.dispatch('increment')调用vuex里异步方法
actions:{increment({commit}){commit('increment')}}调取mutatins的方法
import{mapActions}from'vuex'
methods:{...mapActions(['increment',// 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`// `mapActions` 也支持载荷:'incrementBy'// 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`]),...mapActions({add:'increment'// 将 `this.add()` 映射为 `this.$store.dispatch('increment')`})}