国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

在Vuex使用dispatch和commit來調用mutations的區別詳解

2024-05-06 15:29:53
字體:
來源:轉載
供稿:網友

main.js中

import Vuex from 'vuex'Vue.use(vuex);const store = new Vuex.store({  state: {    nickName: "",    cartCount: 0    },  mutations: {    updateUserInfo(state,nickName) {      state.nickName = nickName;    },    updateCartCount(state,cartCount) {      state.cartCount += cartCount;    }  }, actions: { updateUserInfo(context) {  context.commit("updateUserInfo"); }, updateCartCount(context) {  context.commit("updateCartCount"); } }})new Vue({  el: "#app",  store,  router,  template: '<App/>',  components: {App}})

組件中:

methods: {  increment(){  this.$store.dispatch("updateUserInfo", 'nick'); //this.$store.commit("increment", 'nick');  },  decrement() {  this.$store.dispatch("updateCartCount", 1); // this.$store.commit("decrement", 1);  } } 

區別:

dispatch:含有異步操作,例如向后臺提交數據,寫法: this.$store.dispatch('mutations方法名',值)

commit:同步操作,寫法:this.$store.commit('mutations方法名',值)

以上這篇在Vuex使用dispatch和commit來調用mutations的區別詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 灵丘县| 东丰县| 鄂托克前旗| 大名县| 大宁县| 平武县| 广宗县| 大余县| 和田市| 白沙| 阿拉善右旗| 永康市| 合作市| 维西| 台东县| 亳州市| 广宗县| 大港区| 孟州市| 伊金霍洛旗| 洛浦县| 宁化县| 宜昌市| 遂宁市| 石阡县| 河间市| 抚宁县| 广西| 简阳市| 鄱阳县| 雷山县| 锦屏县| 郁南县| 宁国市| 延边| 闽清县| 京山县| 华容县| 红安县| 瑞金市| 天峨县|