不废话直接上:
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'
Logo

前往低代码交流专区

更多推荐