我已经使用vue/vuex了几个月了,我看到了mapStatemapGettersmapActionsmapMutations,但是除了mapState,他们不知道他们做什么。

这就是我使用mapState的方式

// mutations.js
user: {
   address: {},
   name: '',
   age: ''
}

在我的代码中,我有这样的东西:
import { mapState } from 'vuex'
export default {
  data: {},

  computed: {
   ...mapState({
    address: state => state.user.address
   })
  }
}

那么我可以在任何地方使用地址,但我不知道其他人的用途。

有人可以举一个简单的例子吗?谢谢

最佳答案

这些映射器都不是必需的。他们的目标只是使在组件中创建计算的属性或方法变得容易。它们是最好的DRY(避免重复)。
mapState是一个帮助程序,可简化创建计算的属性的工作,该属性反射(reflect)给定状态的值。

相似地:

  • mapGetters是一个帮助程序,可简化创建计算的属性的工作,该属性反射(reflect)给定getter返回的值。
  • 注意,即使method-style getters也应映射到计算属性。
  • mapActions是一个帮助程序,可简化创建方法的工作,该方法等同于在操作上调用dispatch
  • mapMutations是一个帮助程序,它简化了创建方法的工作,该方法等同于对突变调用commit

  • 由于代码有帮助,下面的演示显示了使用所有这些映射器的示例,以及不使用映射器的等效的示例。他们的行为是完全一样的。映射器仅允许您使用较少的代码编写(请考虑将在应用程序的许多组件中重复执行此操作)。

    const store = new Vuex.Store({
      strict: true,
      state: {name: "John"},
      mutations: {
      	changeName(state, data) {
        	state.name = data
        }
      },
      actions: {
        fetchRandomName({ commit }) {
          let randomId = Math.floor(Math.random() * 12) + 1  ;
          axios.get("https://reqres.in/api/users/" + randomId).then(response => {
            commit('changeName', response.data.data.first_name)
          })
        }
      },
      getters: {
        getName: state => state.name,
        getTransformedName: (state) => (upperOrLower) => {
          return upperOrLower ? state.name.toUpperCase() : state.name.toLowerCase()
        }
      }
    });
    new Vue({
      store,
      el: '#app',
      data: { newName: 'Bob' },
      computed: {
        ...Vuex.mapState(['name']),
        nameNoMapper() { return this.$store.state.name },
        ...Vuex.mapGetters(['getName', 'getTransformedName']),
        getNameNoMapper() { return this.$store.getters.getName },
        getTransformedNameNoMapper() { return this.$store.getters.getTransformedName }
      },
      methods: {
        ...Vuex.mapActions(['fetchRandomName']),
        ...Vuex.mapMutations(['changeName']),
        fetchRandomNameNoMapper() { this.$store.dispatch('fetchRandomName') },
        changeNameNoMapper(newName) { this.$store.commit('changeName', newName) },
      }
    })
    table, tr, td {
      font-family: monospace;
      border: 1px solid black;
      border-collapse: collapse;
    }
    <script src="https://unpkg.com/vue"></script>
    <script src="https://unpkg.com/vuex"></script>
    <script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>
    
    <div id="app">
      <table>
        <tr>
          <td style="width: 250px">With Mappers</td>
          <td style="width: 310px">Without Mappers</td>
        </tr>
        <tr>
          <td>
            name: {{ name }}<br>
            getName: {{ getName }}<br>
            getTransformedName(true): {{ getTransformedName(true) }}<br>
            getTransformedName(false): {{ getTransformedName(false) }}
          </td>
          <td>
            nameNoMapper: {{ nameNoMapper }}<br>
            getNameNoMapper: {{ getNameNoMapper }}<br>
            getTransformedNameNoMapper(true): {{ getTransformedNameNoMapper(true) }}<br>
            getTransformedNameNoMapper(false): {{ getTransformedNameNoMapper(false) }}
          </td>
        </tr>
      </table>
      <hr>
      <button @click="fetchRandomName">ACTION: fetchRandomName</button> - <button @click="fetchRandomNameNoMapper">ACTION: fetchRandomNameNoMapper</button><br>
      <hr>
      <input v-model="newName"><button @click="changeName(newName)">MUTATION: changeName</button><button @click="changeNameNoMapper(newName)">MUTATION: changeNameNoMapper</button>
    </div>

    09-25 16:11