当前位置: 首页 > 工具软件 > store.js > 使用案例 >

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

满昊然
2023-12-01

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

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/slow097/article/details/119884351

 类似资料: