import Vue from "vue" import Vuex from "vuex" Vue.use(Vuex) const state = { //定义访问状态对象 count : 44 } const mutations = { //定义触发状态对象方法,传入state整个对象 jia(state) { state.count ++ } } const getters = { //相似计算属性,对state的数据进行复杂的逻辑计算,使用return返回结果 count : function (state){ return state.count += 100 } } const actions = { //异步执行方法,传入参数context,等同于整个store jianplus (context) { context.commit("jia",10) //调用mutations中的方法并传参 } testplus ({commit}) { //简写方式,使用{} 单独传入mutations对象 commit(‘jian’) //调用mutations中的方法 }
three({commit,dispatch}){ //使用dispatch调用actions的方法
return dispatch('testplus').then(()=>{
commit('jianplus')
})
} }, Const moduleA = { //定义一个模块,引入各个状态对象或方法 state, mutations, getters, actions } export default new Vuex.Store ({ //没有使用模块时的写法 state, mutations, gettrts, actions }) // export default new Vuex.Store ({ //使用模块时的写法 // modules : { // a : moduleA //引入定义模块 // } // })
如何在HTML中访问:当未引入 mapState时,可经过$store去访问 引入时按照正常的变量使用vue
<h1>{{$store.state.count}}</h1> //经过$store去访问
在vue文件中的定义:在计算属性computed位置进行引用,使用mapStatevuex
import {mapState } from ‘Vuex’ //引入 mapState export default { name: 'test', data: () => ({ test: '', }), methods: { }, // computed: mapState([ //直接使用state中定义的count 注意这里是一个数组 // "count" // ]) computed: mapState({ //对state中定义的count作运算后再使用 注意这里是一个对象 count : state => state.count+1 }) }
如何使用:跟事件触发方法同样跟在各类事件以后,经过commit方法触发 数组
<button type="button" @click="$tore.commit(“jia”)"></button>
如何传参:括号内第一个值为方法名,后面为传入参数,传入的参数能够为一个对象异步
<button type="button" @click="$tore.commit(“jia”,10)"></button>
在vue文件中的定义:在定义方法methods位置进行引用,使用mapMutationsthis
import {mapState,mapMutations} from ‘Vuex’ //引入 mapMutations export default { name: 'test', data: () => ({ test: '', }), methods:mapMutations([ //定义mutations中的各个方法 ‘jia’, ‘jian’ ]) computed: mapState({ //对state中定义的count作运算后再使用 count : state => state.count+1 }) }
在vue文件中的定义:在计算属性computed位置进行引用,使用mapGettersspa
computed : { //vue文件中只能有一个computed,同时定义state时,须要改变写法 ...mapState ({ //注意使用... "count” }), count () { //调用getters中的count方法,将值return出去 return this.$store.getters.count }
简化写法:引入mapGetterscode
import {mapState,mapMutations,mapGetters} from ‘Vuex’ //引入 mapGetters export default { name: 'test', data: () => ({ test: '', }), methods:mapMutations([ //定义mutations中的各个方法 ‘jia’, ‘jian’ ]) computed : { //vue文件中只能有一个computed,同时定义state时,须要改变写法 ...mapState ({ //注意使用... "count” }), ...mapGetters([ //调用getters中的count "count” ]) } }
在vue文件中的定义:在定义方法methods位置进行引用,使用mapActions 也能够经过dispatch方法触发 对象
import {mapState,mapMutations,mapGetters,mapActions} from ‘Vuex’ //引入 mapActions export default { name: 'test', data: () => ({ test: '', }), methods:{ ...mapMutations([ ‘jia’, ‘jian’ ]), ...mapActions([ //引入actions中定义的jiaplus方法 ‘jiaplus’ ]) } computed : { ...mapState ({ "count” }), ...mapGetters([ "count” ]) } }
<button type="button" @click="$tore.dispatch(“jiaplus”)"></button>
dispatch表明的是actions的整个对象,咱们能够经过在其中一个actions中传入dispatch触发其余的actions方法,会有一个返回值
actions: { actionA ({ commit }) { return new Promise((resolve, reject) => { setTimeout(() => { commit('someMutation') resolve() }, 1000) }) }, actionB ({ dispatch, commit }) { // 组合,传入dispatch 调用actions的其余方法 return dispatch('actionA').then(() => { commit('someOtherMutation') }) } }