vuex的commit

 

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

store 中的状态的唯一方法是提交 mutation 

store.commit('increment')

 

----------------------------------------------------------

 

Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation [context 对象不是 store 实例本身了]

actions: {
  increment ({ commit }) {
    commit('increment')
  }
}

 

----------------------------------------------------------

 

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


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


this.$store.commit():同步操作,例如向后台提交数据,写法:this.$store.commit(‘mutations方法名’,值)

 

commit: 同步操作

存储 this.$store.commit('changeValue',name)
取值 this.$store.state.changeValue


dispatch: 异步操作

存储 this.$store.dispatch('getlists',name)
取值 this.$store.getters.getlists


存储一样 取值方式不同罢啦

 

转 :  https://blog.csdn.net/u011565547/article/details/108830084

 

posted @ 2021-08-28 10:12  与f  阅读(836)  评论(0编辑  收藏  举报