vuex中 this.$store.dispatch() 与 this.$store.commit()方法的区别

1: this.$store.dispatch() 与 this.$store.commit()方法的区别总的来说他们只是存取方式的不同,两个方法都是传值给vuex的mutation改变state。

2: this.$store.dispatch() :含有异步操作,例如向后台提交数据, 写法this.$store.dispatch(‘action方法名’,值);

3: commit 是同步操作。
   存储 this.$store.commit('changeValue',name);
   取值 this.$store.state.changeValue

4: dispatch: 异步操作   dispatch 是异步操作。
   存储 this.$store.dispatch('getlists',name);
   取值 this.$store.getters.getlists;

5: Vuex文件 src/store/index.js
   import Vue from "vue";
   import Vuex from "vuex";

   Vue.use(Vuex);

   
export const store = new Vuex.Store({
  // state专门用来保存 共享的状态值
  state: {
    // 保存登录状态
    login: false
  },
  // mutations: 专门书写方法,用来更新 state 中的值
  mutations: {
    // 登录
    doLogin(state) {
      state.login = true;
    },
    // 退出
    doLogout(state) {
      state.login = false;
    }
  }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值