About Vue's 4 auxiliary functions of Vuex

About Vue's 4 auxiliary functions of Vuex

1. Auxiliary functions

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, saving you a few keystrokes.

Introduce auxiliary functions into script:

import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'

2. Examples

1. mapState and mapGetters

Because state and getters return properties, that is, specific values, mapState and mapGetters should be placed in the computed property computed .

For example:

Now there is a property userName in the store state:

In the Home.vue component, it is obtained through mapState and displayed on the interface:

Code in computed:

  computed:{
    //Get the state in the store through the auxiliary function
    ...mapState(['userName'])
    //Equivalent to: the following regular calculated property code /* userName (){
      return this.$store.state.userName
    }*/
  }

Calling in the page:

result:

In this way, you can use a simple ...mapState(['userName']) to replace the relatively long calculated property function.
The usage of mapGetters is the same as that of mapState . You can call it in computed . Here we only take mapState as an example.

2. mapMutations and mapActions

Because mutations and actions return functions, they should be placed in the methods property of the component.

For example:

Now in store 's mutations , there is a tip function that pops up a welcome window for the current user:

The code in methods in the figure above is a shorthand for getting the mutation in the store.

  methods:{
    //Abbreviation for getting mutations in the store
    ...mapMutations(['tip'])
    //Equivalent to /* tip(){
      this.$store.commit('tip');
    }*/
  }

Call in component: Call the tip method in the created function to implement the welcome pop-up window.

The use of mapActions and mapMutations is the same, that is, they can be declared in nethods , which simplifies referencing actions and mutations in the store.

As you can see from the above example, the benefit of auxiliary functions is that they help us simplify getting the state, getter, mutation, and action in the store. Of course, the above functions can be achieved without using auxiliary functions, but when multiple states need to be used simultaneously in a component, auxiliary functions become more convenient.

This concludes this article about the four auxiliary functions of Vuex of Vue. For more relevant Vuex auxiliary function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  Detailed explanation of command to view log files in Linux environment

>>:  Meta tags in simple terms

Recommend

Use of marker tags in CSS list model

This article mainly introduces the ::master pseud...

The meaning of the 5 types of spaces in HTML

HTML provides five space entities with different ...

React-Native environment setup and basic introduction

Environment Preparation 1. Environment Constructi...

Windows system mysql5.7.18 installation graphic tutorial

MySQL installation tutorial for Windows system do...

A simple method to be compatible with IE6's min-width and min-height

If a website is widescreen, you drag the browser ...

mysql join query (left join, right join, inner join)

1. Common connections for mysql INNER JOIN (inner...

Summary of commonly used commands for docker competition submission

Log in to your account export DOCKER_REGISTRY=reg...

Introduction to Semantic HTML Tags

In the past few years, DIV+CSS was very popular in...

JavaScript to achieve a simple page countdown

This article example shares the specific code of ...

Web front-end development course What are the web front-end development tools

With the development of Internet technology, user...

Boundary and range description of between in mysql

mysql between boundary range The range of between...

Three Vue slots to solve parent-child component communication

Table of contents Preface Environment Preparation...

The difference between char and varchar in MYSQL

CHAR and VARCHAR types are similar, differing pri...

How to implement gzip compression in nginx to improve website speed

Table of contents Why use gzip compression? nginx...