Vuex知识点:State、Mutations、Actions
1.vuex的存储是响应式的,当store的状态发生改变时,那么组件中的状态也会跟着相应改变。
2.store的状态是不可以直接改变的,改变store的唯一办法是通过commit mutation。
3.为了使所有的子组件不用频繁注入store,vue提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex))
State
1 /main.js 2 import store from './store/' 3 vue.use(vuex) 4 new Vue({ 5 router, 6 store, 7 }).$mount('#app') 8 通过在根组件中注入store,我们可以在子组件中this.$store 访问store中的状态(state) 9 10 /store/index.js 11 import Vue from 'vue' 12 import Vuex from 'vuex' 13 const state = { 14 count1: 1, 15 count2: 2 16 } 17 export default new Vuex.Store({ 18 state 19 }) 20 21 /子组件 22 html: 23 <p>{{count1}}</p> 24 <p>{{count2}}</p> 25 26 js: 27 export default { 28 computed: { 29 count1 () { 30 return this.$store.state.count1 31 }, 32 count2 () { 33 return this.$store.state.count2 34 }, 35 } 36 }
mapState 辅助函数
在子组件获取store的方法有很多,最常见的方法是就是上面介绍的
但是一旦想要获取多个状态值就需要声明很多个计算属性,这样就会让代码显得很冗余(上面获取两个状态,就需要写两个函数)
可以使用 mapState 辅助函数帮助我们生成计算属性,使代码看起来很简洁
方式一:
1 html: 2 <p>{{count1}}</p> 3 <p>{{count2}}</p> 4 5 js: 6 export default { 7 computed: { 8 ...mapState(['count1','count2']) 9 } 10 }
方式二:
1 html: 2 <p>{{countAlias1}}</p> 3 <p>{{countAlias2}}</p> 4 5 js: 6 export default { 7 computed: mapState({ 8 countAlias1: 'count1', 9 countAlias2: 'count2', 10 }) 11 }
方式三:
1 html: 2 <p>{{count1}}</p> 3 <p>{{count2}}</p> 4 5 js: 6 export default { 7 computed: mapState({ 8 count1: state => state.count1 9 count2: state => state.count2 10 //完整是这样子的,以上是简写:count1 = function (state) { return state.count1 } 11 }) 12 }
mutations
想要更改state的状态值,只能通过commit mutations的方法来更改。
比如我想点击验证按钮的时候,下面的p标签的插值表达式{{datatext}}状态值会变化。
1 /html 2 <button @click="change">验证</button> 3 <p>{{datatext}}</p> 4 5 /store/index.js 6 7 import Vue from 'vue' 8 import Vuex from 'vuex' 9 import mutations from './mutations'; 10 const state = { 11 datatext: '我还没有改变' 12 } 13 export default new Vuex.Store({ 14 state, 15 mutations 16 })
使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式。
1 /mutation-types.js 2 3 export const DATATEXT_MUTATION = 'DATATEXT_MUTATION'
1 /mutations.js 2 3 import { DATATEXT_MUTATION } from './mutation-types'; 4 export default { 5 // 定义mutations函数,第一个参数state(状态值)、第二个参数是载荷 6 [DATATEXT_MUTATION](state,res) { 7 // 改变state中的datatext状态值 8 state.datatext = res 9 } 10 }
1 /html 2 <button @click="change">验证</button> 3 <p>{{datatext}}</p> 4 5 组件下的js 6 7 export default { 8 methods: { 9 // 将this.$store.commit("DATATEXT_MUTATION")映射为this.DATATEXT_MUTATION() 10 // 通俗点讲就是用this.datatext_mutation()代替this.$store.dispatch("datatext_mutation") 11 ...mapMutations(['DATATEXT_MUTATION']), 12 change() { 13 this.DATATEXT_MUTATION('我改变了状态值') 14 } 15 } 16 17 }
mapMutations辅助函数
在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。
acitons
actions类似于mutations,但是actions可以用异步,但是mutations只能用于同步。
用一个倒计时的例子来加深actions的使用,还是刚刚那个mutations的例子
1 /html 2 <button @click="change">验证</button> 3 <p>{{datatext}}</p> 4 5 /store/index.js 6 import Vue from 'vue' 7 import Vuex from 'vuex' 8 import mutations from './mutations'; 9 import actions from './action'; 10 const = { 11 datatext: '我还没有改变' 12 } 13 export default new Vuex.Store({ 14 state, 15 mutations, 16 actions 17 }) 18 19 /mutation-types.js 20 export const DATATEXT_MUTATION = 'DATATEXT_MUTATION' 21 22 /mutations.js 23 import { DATATEXT_MUTATION } from './mutation-types'; 24 export default { 25 // 定义mutations函数,第一个参数state(状态值)、第二个参数是载荷 26 [DATATEXT_MUTATION](state,res) { 27 // 改变state中的datatext状态值 28 state.datatext = res 29 } 30 }
在service里面定义一个倒计时方法
1 /service 2 export const doubleAfter2seconds = () => { 3 return new Promise((resolve, reject) => { 4 setTimeout(() => { 5 resolve('2s后我被改变了') 6 }, 2000); 7 } ) 8 }
1 /actions.js 2 import {DATATEXT_MUTATION} from './mutation-types' 3 import {doubleAfter2seconds} from './service' 4 5 export default { 6 async datatext_mutation({ 7 commit 8 }) { 9 let res = await doubleAfter2seconds() 10 commit(DATATEXT_MUTATION,res) 11 } 12 }
在组件里使用action
1 export default { 2 methods: { 3 // ...mapActions(['datatext_mutation']), 4 change() { 5 this.$store.dispatch("datatext_mutation"); 6 } 7 } 8 }
mapActions
可以使用辅助函数mapActions将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store):
1 methods: { 2 ...mapActions(['datatext_mutation']), 3 change() { 4 this.datatext_mutation() 5 } 6 }

浙公网安备 33010602011771号