Skip to main content

mapState, mapGetters, mapMutations, mapActions in vuex

 The above four objects are all in the vuex plug-in store, which is often used to store some data shared globally. But I still feel a little uncomfortable when using it, that is, I wrote a lot of repeated prefixes. At this time, these four objects appeared, which solved this pain point very well. mapState is mainly used to manipulate and obtain data, mapGetters is equivalent to computed properties, and mapMutations and mapActions are simplifications of mutation and action. They can be written as both objects and arrays.

Steps for usage

  • Install vuex: >>>npm install vuex (note the version, vue2-vuex3, vue3-vuex4)
  • Vue officially recommends writing an index.js file in the store folder, configuring various configuration items, then registering it on Vue, and exposing the ones to be used.

import Vue from "vue"
import Vuex from "vuex"
const actions={
  addOddnum(context,value){
            context.commit("Idnum",value)
       /*  console.log("Act1...")
        context.dispatch("Act1",value) */
    },
    //
    /* Act1(context,value){
        console.log("Act2...")
        context.dispatch("Act2",value)
    },
    Act2(context,value){
        console.log("Act2 Now do it")
            context.commit("AddOddnum",value)
    }, */
 
}
const mutations={
    Idnum(state,value){
        console.log(state)
        console.log(value)//default enents
        state.sum+=value
    },
...  
}
//
const getters={
    he(state,b,c,d){
       /*  console.log(state)
        console.log(b)//undefined
        console.log(c)
        console.log(d) */
        return state.id+100
    }
}
//
const state={
    id:100,
    name:"aaa"
        }

Vue.use(Vuex)
export default new Vuex.Store({
    actions,
    mutations,
    getters,
    state
})

  • Introduced in main.js
import store from "./store/index"

Vue.config.productionTip = false;
const vm=new Vue({
  render: h => h(App),
  store,
}).$mount('#app');

  • It can work next

 computed:{
      // ...mapState({key:value,...})
      ...mapState([value,...]),
      // ...mapGetters(["value"])
      ...mapGetters({key:value}),
      //
  },
methods: {
 
     ...mapMutations({key:value}),
   
     ...mapActions({key:value})
  },

Comments

Popular posts from this blog

favicon.ico 404 Not Found Causes and Solutions

 What is favicon? It is the abbreviation of Favorites Icon. Its function is that in addition to displaying the corresponding title in the browser's favorites, icons can also be used to distinguish different websites. When I encounter this kind of error, I feel very uncomfortable. There is obviously no problem. Why is a 404 error reported? The following is the solution.

Destructuring assignment in ES6

 In the past, when we wanted to assign a value to a variable, such as an array type and an object type, to assign a value to a variable, we could only specify the value directly. It is very troublesome to write in this way, but under the ES6 syntax specification, it is allowed to directly extract the required values from arrays and objects according to a certain pattern, and directly assign variables to variables. This method is called Destructuring, which is simple to understand. That is, the left and right sides of the equal sign are equal.