vuex中辅助函数(mapState ,mapGetters ,mapMutations,mapActions)
📅 2026/7/16 1:20:34
👁️ 阅读次数
📝 编程学习
不废话直接上:
mapState 与 mapGetters 要放在计算属性computed中。
要使用这几个辅助函数首先要引入:
import { mapState,mapMutations } from 'vuex'computed: { 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 { mapState,mapMutations } from 'vuex'
编程学习
技术分享
实战经验