vuex中的commit、dispatch于store中mutation和action的用法
发布时间
阅读量:
阅读量
建议先读文档再读次文vuex文档

computed: {
...mapState(['count']),
...mapState(['showNum']),
},
methods: {
...mapMutations(['count']),
...mapActions(['subAsync'])
}
AI写代码
commit的作用,就是调用store中某个mutation的函数。
this.$store.commit('函数名', 入参)
AI写代码
dispatch的作用,专门用来触发action
this.$store.dispatch('函数名')
AI写代码
store中,只有mutations中定义的函数,才有权利修改state中的数据
actions: {
add(a, b) {
a.commit('addN', b)
}
}
AI写代码
触发action的方法


Getter
只对数据起到包装作用,不会改变。
使用方法一:
gatters: {
showNum(state) {
return 'XXXXXXXXXXXXXXXXX'
}
}
AI写代码
<h1>{{ &store.getters.showNum }}</h1>
AI写代码
使用方法二:
调用同action方法二相同。
<h1>{{ showNum }}</h1>
AI写代码
全部评论 (0)
还没有任何评论哟~
