最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vuex如何在非組件中調(diào)用mutations方法

 更新時(shí)間:2022年03月28日 11:43:34   作者:asing1elife  
這篇文章主要介紹了vuex如何在非組件中調(diào)用mutations方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在非組件中調(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屬性中的每個(gè)方法中有兩個(gè)參數(shù),分比為state和payload;state其實(shí)就是vuex中的state屬性,payload叫做mutations的載荷,其實(shí)就是傳過來的值。一般payload傳的是一個(gè)對象,這樣可以包含多個(gè)字段并且記錄的 mutation 會(huì)更易讀:

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)格的提交方式,整個(gè)對象都作為載荷傳給 mutation 函數(shù),因此 handler 保持不變:

mutations: {
? increment (state, payload) {
? ? state.count += payload.amount
? }
}

使用常量替代 Mutation 事件類型

使用常量替代 mutation 事件類型在各種 Flux 實(shí)現(xiàn)中是很常見的模式。這樣可以使 linter 之類的工具發(fā)揮作用,同時(shí)把這些常量放在單獨(dú)的文件中可以讓你的代碼合作者對整個(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ì)算屬性命名功能來使用一個(gè)常量作為函數(shù)名
? ? [SOME_MUTATION] (state) {
? ? ? // mutate state
? ? }
? }
})

用不用常量取決于你——在需要多人協(xié)作的大型項(xiàng)目中,這會(huì)很有幫助。但如果你不喜歡,你完全可以不這樣做。

Mutation 必須是同步函數(shù)

一條重要的原則就是要記住 mutation 必須是同步函數(shù)。為什么?請參考下面的例子:

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ù)還沒有被調(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)文章

最新評論

界首市| 连云港市| 黑水县| 株洲县| 乌恰县| 岳普湖县| 凤翔县| 红河县| 台山市| 永新县| 绥宁县| 台州市| 马边| 徐闻县| 察隅县| 安义县| 北辰区| 镇康县| 长岭县| 江油市| 沧州市| 建宁县| 西安市| 遵义市| 比如县| 锡林郭勒盟| 元江| 霍州市| 永修县| 阳高县| 集贤县| 江孜县| 固镇县| 泰安市| 卓资县| 通辽市| 连州市| 吉安市| 双柏县| 东乡县| 灌南县|