Vue front-end development auxiliary function state management detailed example

Vue front-end development auxiliary function state management detailed example

mapState

When a component needs to obtain multiple states, declaring all of these states as calculated properties will be somewhat repetitive and redundant. To solve this problem, we can use the mapState helper function to help us generate calculated properties. When mapState is not used, the object state is obtained, usually in the computes property of the component being used, and is used as follows:

  //....
  computed: {
        count: function(){
            return this.$store.state.count
        }
    }
 //....    

Using mapState this can be simplified to:

import { mapState } from 'vuex' //Introduce mapState object export default {
  // ...
  computed: mapState({
    // Arrow functions can make the code more concise count: state => state.count,
  })
}
Or import { mapState } from 'vuex' //Introduce mapState object export default {
  // ...
  computed: mapState({
    'count', //consistent with the state name countAlias: 'count' //countAlias ​​is the alias used in the reference component})
}

mapGetters

The mapGetters helper function simply maps getters in the store to local computed properties, similar to state. The calculation function code is simplified to:

import { mapGetters } from 'vuex'
export default {
  // ...
  computed: {
  // Use the object spread operator to mix getters into the computed object...mapGetters([
      'countDouble',
      'CountDoubleAndDouble',
      //..
    ])
  }
}

MapGetters can also use aliases.

mapMutations

Use the mapMutations helper function to map the methods in the component to store.commit calls. The simplified code is:

import { mapMutations } from 'vuex'
export default {
  //..
  methods: {
    ...mapMutations([
      'increment' // Map this.increment() to this.$store.commit('increment')
    ]),
    ...mapMutations({
      add: 'increment' // Map this.add() to this.$store.commit('increment')
    })
  }
}

mapActions

Use the mapActions helper function to map component methods to store.dispatch calls. The simplified code is:

import { mapActions } from 'vuex'
export default {
  //..
  methods: {
    ...mapActions([
      'incrementN' //Map this.incrementN() to this.$store.dispatch('incrementN')
    ]),
    ...mapActions({
      add: 'incrementN' //Map this.add() to this.$store.dispatch('incrementN')
    })
  }
}

Example

Following the example in Vue State Management (II), this is done using auxiliary functions. Auxiliary functions are used in the CountChange and ComputeShow components, and the rest of the code does not need to be changed.
In ComputeShow, two auxiliary functions, mapState and mapGetters, are used. The code is as follows

<template>
  <div align="center" style="background-color: bisque;">
    <p>The following is how to use computed to directly obtain the status data in stores, and refresh it synchronously when the status data changes</p>
    <h1>Use computed to receive state: {{ computedState }}</h1>
    <h1>Use computed to receive Getters: {{ computedGetters }}</h1>
  </div>
</template>
<script>
  import { mapState,mapGetters } from 'vuex' //Introduce mapState,mapGetters objects export default {
    name: 'ComputeShow',
    computed:{
    ...mapState({
      computedState:'count' //Alias: computedState
    }),
    ...mapGetters({
      computedGetters:'getChangeValue' //Alias: computedGetters
    })
    }
  }
</script>
<style>
</style>

It is recommended to add an alias when using a map, so that it is decoupled from the contents of the stores. In CountChange, two auxiliary functions, mapMutations and mapActions, are used. The code is as follows

<template>
  <div align="center">
    <input type="number" v-model="paramValue" />
    <button @click="addNum({res: parseInt(paramValue)})">+Increase</button>
    <button @click="subNum">-Decrease</button>
  </div>
</template>
<script>
  import {
    mapMutations,
    mapActions
  } from 'vuex' //Introduce mapMutations, mapActions objects export default {
    name: 'CountChange',
    data() {
      return {
        paramValue: 1,
      }
    },
    methods: {
      ...mapMutations({
        subNum: 'sub' //Add alias subNum
      }),
      ...mapActions({
        addNum: 'increment' //Map this.incrementN() to this.$store.dispatch('incrementN')
      })
    }
  }
</script>
<style>
</style>

Similarly, give aliases to methods in stores, and when parameters need to be passed, pass them to actions or mutations through aliases. For example, "addNum({res: parseInt(paramValue)})" sends an object {res: ''}

summary

The auxiliary function itself has no new meaning and is mainly used to simplify the code when using State, Getters, Mutations, and Actions.

The above is the details of the detailed example of the state management of auxiliary functions in Vue front-end development. For more information about the state management of Vue auxiliary functions, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to use provide to implement state management in Vue3
  • The practical process of login status management in the vuex project
  • Quickly master how to get started with Vuex state management in Vue3.0
  • How to understand Vue's simple state management store mode
  • Application of mapState idea in vuex
  • How to use vuex in Vue project
  • Use of vuex namespace
  • Vue state management: using Pinia instead of Vuex

<<:  Docker image creation, uploading, pulling and deployment operations (using Alibaba Cloud)

>>:  How to enable MySQL remote connection

Recommend

Summary of MySQL InnoDB locks

Table of contents 1. Shared and Exclusive Locks 2...

Flex layout realizes the layout mode of upper and lower fixed and middle sliding

This article mainly introduces the layout method ...

Nginx defines domain name access method

I'm building Nginx recently, but I can't ...

MySQL 8.0.15 installation graphic tutorial and database basics

MySQL software installation and database basics a...

CSS realizes the mask effect when the mouse moves to the image

1. Put the mask layer HTML code and the picture i...

Two-hour introductory Docker tutorial

Table of contents 1.0 Introduction 2.0 Docker Ins...

MySQL replication table details and example code

MySQL replication table detailed explanation If w...

17 JavaScript One-Liners

Table of contents 1. DOM & BOM related 1. Che...

Two simple ways to remove text watermarks from web pages

<br /> When we browse certain websites and s...

MySQL batch removes spaces in a certain field

Is there any way to remove spaces from a certain ...

Analysis of uniapp entry-level nvue climbing pit record

Table of contents Preface Hello World image Set b...

Vue implements scroll loading table

Table of contents Achieve results Rolling load kn...

Two ways to exit bash in docker container under Linux

If you want to exit bash, there are two options: ...