vuex中辅助函数(mapState ,mapGetters ,mapMutations,mapActions)

发布时间:2026/7/16 1:15:09
vuex中辅助函数(mapState ,mapGetters ,mapMutations,mapActions) 不废话直接上mapState 与 mapGetters 要放在计算属性computed中。要使用这几个辅助函数首先要引入import { mapStatemapMutations } from vuexcomputed: { localComputed () { /* ... */ }, // 使用对象展开运算符将此对象混入到外部对象中 ...mapState({ // ... }) }mapMutations与mapActions要放在methods中。export default { // ... methods: { ...mapMutations([ increment, // 将 this.increment() 映射为 this.$store.commit(increment) // mapMutations 也支持载荷 incrementBy // 将 this.incrementBy(amount) 映射为 this.$store.commit(incrementBy, amount) ]), ...mapMutations({ add: increment // 将 this.add() 映射为 this.$store.commit(increment) }) } }当然了需要引入import { mapStatemapMutations } from vuex