淺談vuex之mutation和action的基本使用
我們要實(shí)現(xiàn)的很簡(jiǎn)單,就是點(diǎn)擊+1的count加一,點(diǎn)擊-1的時(shí)候count-1
一、mutation
在vue 中,只有mutation 才能改變state. mutation 類(lèi)似事件,每一個(gè)mutation都有一個(gè)類(lèi)型和一個(gè)處理函數(shù),因?yàn)橹挥衜utation 才能改變state, 所以處理函數(shù)自動(dòng)會(huì)獲得一個(gè)默認(rèn)參數(shù) state. 所謂的類(lèi)型其實(shí)就是名字,action去commit 一個(gè)mutation, 它要指定去commit哪個(gè)mutation, 所以mutation至少需要一個(gè)名字,commit mutation 之后, 要做什么事情,那就需要給它指定一個(gè)處理函數(shù), 類(lèi)型(名字) + 處理函數(shù)就構(gòu)成了mutation. 現(xiàn)在test.js添加mutation.
const store = new Vuex.Store({ state: { count:0 }, mutations: { // 加1 increment(state) { state.count++; }, // 減1 decrement(state) { state.count-- } } })
Vue 建議我們mutation 類(lèi)型用大寫(xiě)常量表示,修改一下,把mutation 類(lèi)型改為大寫(xiě)
mutations: { // 加1 INCREMENT(state) { state.count++; }, // 減1 DECREMENT(state) { state.count-- } }
二、action
action去commit mutations, 所以還要定義action. test.js 里面添加actions.
const store = new Vuex.Store({ state: { count:0 }, mutations: { // 加1 INCREMENT(state) { state.count++; }, // 減1 DECREMENT(state) { state.count-- } }, actions: { increment(context) { context.commit("INCREMENT"); }, decrement(context) { context.commit("DECREMENT"); } } })
action 和mutions 的定義方法是類(lèi)似的,我們要dispatch 一個(gè)action, 所以actions 肯定有一個(gè)名字,dispatch action 之后它要做事情,就是commit mutation, 所以還要給它指定一個(gè)函數(shù)。因?yàn)橐猚ommit mutation ,所以 函數(shù)也會(huì)自動(dòng)獲得一個(gè)默認(rèn)參數(shù)context, 它是一個(gè)store 實(shí)例,通過(guò)它可以獲取到store 實(shí)例的屬性和方法,如 context.state 就會(huì)獲取到 state 屬性, context.commit 就會(huì)執(zhí)行commit命令。
其實(shí)actions 還可以簡(jiǎn)寫(xiě)一下, 因?yàn)楹瘮?shù)的參數(shù)是一個(gè)對(duì)象,函數(shù)中用的是對(duì)象中一個(gè)方法,我們可以通過(guò)對(duì)象的解構(gòu)賦值直接獲取到該方法。修改一下 actions
actions: { increment({commit}){ commit("INCREMENT") }, decrement({commit}){ commit("DECREMENT") } }
三、dispatch action
現(xiàn)在就剩下dispatch action 了。什么時(shí)候dispatch action 呢? 只有當(dāng)我們點(diǎn)擊按鈕的時(shí)候. 給按鈕添加click 事件,在click 事件處理函數(shù)的中dispatch action.
這個(gè)時(shí)候我們需要新建一個(gè)操作組件,我們暫且命名為test.vue
<template> <div> <div> <button @click="add">+1</button> <button @click="decrement">-1</button> </div> </div> </template>
然后,我們?cè)趍ethods里面獲取這兩個(gè)操作事件
<script> export default { methods: { increment(){ this.$store.dispatch("increment"); }, decrement() { this.$store.dispatch("decrement") } } } </script>
當(dāng)然上面這種寫(xiě)法比較麻煩,vuex還給我我們提供了mapActions這個(gè)函數(shù),它和mapState 是一樣的,把我們的 action 直接映射到store 里面的action中。
<script> import {mapActions} from 'vuex' export default { methods: { ...mapActions(['increment', 'decrement']) } } </script>
如果事件處理函數(shù)名字和action的名字不同,給mapActions
提供一個(gè)對(duì)象,對(duì)象的屬性是事件處理函數(shù)名字, 屬性值是 對(duì)應(yīng)的dispatch 的action 的名字。
<script> import {mapActions} from 'vuex' export default { methods: { // 這中寫(xiě)法雖然可行,但是比較麻煩 // 這時(shí)vue 提供了mapAction 函數(shù), // 它和mapState 是一樣的,把我們的 action 直接映射到store 里面的action中。 // increment () { // this.$store.dispatch('increment') // }, // decrement () { // this.$store.dispatch('decrement') // } // 下面我們使用一種比較簡(jiǎn)潔的寫(xiě)法 // ...mapActions(['increment', 'decrement']) /** 如果事件處理函數(shù)名字和action的名字不同,給mapActions 提供一個(gè)對(duì)象,對(duì)象的屬性是事件處理函數(shù)名字, 屬性值是 對(duì)應(yīng)的dispatch 的action 的名字。 */ // 這里實(shí)際是為了改變事件的名字 ...mapActions(['decrement']), ...mapActions({ add: 'increment' }) } } </script>
這時(shí)候我們單擊按鈕,就可以看到count 發(fā)生變化。
最后附一張簡(jiǎn)單的圖形解析,看起來(lái)應(yīng)該能更直觀一點(diǎn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element-ui中的clickoutside點(diǎn)擊空白隱藏元素
這篇文章主要為大家介紹了element-ui中的clickoutside點(diǎn)擊空白隱藏元素示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03vue項(xiàng)目中仿element-ui彈框效果的實(shí)例代碼
這篇文章主要介紹了vue項(xiàng)目中仿element-ui彈框效果的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04VS?Code打開(kāi)vue文件出現(xiàn)很多黃色波浪線的完美解決辦法
作為一名經(jīng)驗(yàn)豐富的開(kāi)發(fā)者,下面這篇文章主要給大家介紹了關(guān)于VS?Code打開(kāi)vue文件出現(xiàn)很多黃色波浪線的完美解決辦法,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04Vue實(shí)現(xiàn)實(shí)時(shí)更新sessionStorage數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細(xì)介紹了Vue如何實(shí)現(xiàn)實(shí)時(shí)更新sessionStorage數(shù)據(jù),文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以參考一下2023-06-06使用props傳值時(shí)無(wú)法在mounted處理的解決方案
這篇文章主要介紹了使用props傳值時(shí)無(wú)法在mounted處理的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04