vuex如何在非組件中調(diào)用mutations方法
在非組件中調(diào)用mutations方法
一般情況下調(diào)用 mutations.js 中的方法都是在組件中,如果想在非組件中調(diào)用,則需要使用如下方式
在組件中調(diào)用
import {mapMutations} from 'vuex' import {SET_IS_LOGIN} from 'store/mutation-types' export default { ? ? methods: { ? ? ? ? ...mapMutations({ ? ? ? ? ? ? set_is_login: SET_IS_LOGIN ? ? ? ? }), ? ? ? ? init() { ? ? ? ? ? ? this.set_is_login(true) ? ? ? ? } ? ? } }
在非組件中調(diào)用
import store from 'store' import {SET_IS_LOGIN} from 'store/mutation-types' function init() { ? ? store.commit(SET_IS_LOGIN, true) }
vuex的mutations屬性
mutations屬性介紹
是唯一一種方式來(lái)修改state中的狀態(tài)的;在組件的自定義方法中,使用this.$store.commit(‘對(duì)應(yīng)mutations中的方法’, 新的值)方法,把新的值提交給mutations中相對(duì)應(yīng)的方法,mutations屬性中的每個(gè)方法中有兩個(gè)參數(shù),分比為state和payload;state其實(shí)就是vuex中的state屬性,payload叫做mutations的載荷,其實(shí)就是傳過(guò)來(lái)的值。一般payload傳的是一個(gè)對(duì)象,這樣可以包含多個(gè)字段并且記錄的 mutation 會(huì)更易讀:
mutations: { ? increment (state, payload) { ? ? state.count += payload.amount ? } }
store.commit('increment', { ? amount: 10 })
對(duì)象風(fēng)格的提交方式
提交 mutation 的另一種方式是直接使用包含 type 屬性的對(duì)象:
store.commit({ ? type: 'increment', ? amount: 10 })
當(dāng)使用對(duì)象風(fēng)格的提交方式,整個(gè)對(duì)象都作為載荷傳給 mutation 函數(shù),因此 handler 保持不變:
mutations: { ? increment (state, payload) { ? ? state.count += payload.amount ? } }
使用常量替代 Mutation 事件類型
使用常量替代 mutation 事件類型在各種 Flux 實(shí)現(xiàn)中是很常見(jiàn)的模式。這樣可以使 linter 之類的工具發(fā)揮作用,同時(shí)把這些常量放在單獨(dú)的文件中可以讓你的代碼合作者對(duì)整個(gè) app 包含的 mutation 一目了然:
// mutation-types.js export const SOME_MUTATION = 'SOME_MUTATION'
// store.js import Vuex from 'vuex' import { SOME_MUTATION } from './mutation-types'
const store = new Vuex.Store({ ? state: { ... }, ? mutations: { ? ? // 我們可以使用 ES2015 風(fēng)格的計(jì)算屬性命名功能來(lái)使用一個(gè)常量作為函數(shù)名 ? ? [SOME_MUTATION] (state) { ? ? ? // mutate state ? ? } ? } })
用不用常量取決于你——在需要多人協(xié)作的大型項(xiàng)目中,這會(huì)很有幫助。但如果你不喜歡,你完全可以不這樣做。
Mutation 必須是同步函數(shù)
一條重要的原則就是要記住 mutation 必須是同步函數(shù)。為什么?請(qǐng)參考下面的例子:
mutations: { ? someMutation (state) { ? ? api.callAsyncMethod(() => { ? ? ? state.count++ ? ? }) ? } }
現(xiàn)在想象,我們正在 debug 一個(gè) app 并且觀察 devtool 中的 mutation 日志。每一條 mutation 被記錄,devtools 都需要捕捉到前一狀態(tài)和后一狀態(tài)的快照。然而,在上面的例子中 mutation 中的異步函數(shù)中的回調(diào)讓這不可能完成:因?yàn)楫?dāng) mutation 觸發(fā)的時(shí)候,回調(diào)函數(shù)還沒(méi)有被調(diào)用,devtools 不知道什么時(shí)候回調(diào)函數(shù)實(shí)際上被調(diào)用——實(shí)質(zhì)上任何在回調(diào)函數(shù)中進(jìn)行的狀態(tài)的改變都是不可追蹤的。
在組件中提交 Mutation
你可以在組件中使用 this.$store.commit(‘xxx’) 提交 mutation,或者使用 mapMutations 輔助函數(shù)將組件中的 methods 映射為 store.commit 調(diào)用(需要在根節(jié)點(diǎn)注入 store)。
import { mapMutations } from 'vuex' export default { ? // ... ? 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')` ? ? }) ? } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
v-if 導(dǎo)致 elementui 表單校驗(yàn)失效問(wèn)題解決方案
在使用 elementui 表單的過(guò)程中,某些表單項(xiàng)需要通過(guò) v-if 來(lái)判斷是否展示,但是這些表單項(xiàng)出現(xiàn)了檢驗(yàn)失效的問(wèn)題,今天小編給大家介紹v-if 導(dǎo)致 elementui 表單校驗(yàn)失效問(wèn)題解決方案,感興趣的朋友一起看看吧2024-01-01使用elementUI table展開(kāi)行內(nèi)嵌套table問(wèn)題
這篇文章主要介紹了使用elementUI table展開(kāi)行內(nèi)嵌套table問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue-element的select下拉框賦值實(shí)例
這篇文章主要介紹了vue-element的select下拉框賦值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03vue-cli3 項(xiàng)目?jī)?yōu)化之通過(guò) node 自動(dòng)生成組件模板 generate View、Component
這篇文章主要介紹了vue-cli3 項(xiàng)目?jī)?yōu)化之通過(guò) node 自動(dòng)生成組件模板 generate View、Component的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Vue自定義表單驗(yàn)證(rule,value,callback)使用詳解
這篇文章主要介紹了Vue自定義表單驗(yàn)證(rule,value,callback)使用詳解,今天我們講一講自定義驗(yàn)證規(guī)則具體使用場(chǎng)景和它的三個(gè)參數(shù)意思和使用,需要的朋友可以參考下2023-07-07