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