Detailed explanation of Getter usage in vuex

Detailed explanation of Getter usage in vuex

Preface

Vuex allows us to define "getters" in the store (which can be thought of as computed properties of the store). Just like computed properties, the return value of a getter is cached based on its dependencies and is only recomputed if the values ​​of its dependencies change.

Take the example of the official website as an example. The official website code is as follows:

Use the following code to access it in the component:

this.$store.getters.doneTodosCount

1. Description

The accessor function in getters will pass two parameters (state, getters) by default. The first parameter state can be used to access the data, and the getters parameter can be used to access other accessor functions in the accessor. In most cases, only the first parameter is needed. When defining an accessor function, just write the first parameter, as in the example above. When accessing these accessor properties, they are called just like computed properties in the component, rather than like function calls.

This is an example of using the second parameter getters. Use the following code directly in the component to call it, just like calling a calculated property. The second parameter will be passed by default.

this.$store.getters.doneTodos

2. Getter returns a function

Passing parameters to the getter is accomplished by having the getter return a function. Therefore, its main function is to pass parameters.

When this.$store.getters.getTodoById is accessed directly in a component, a function is returned. Then pass the parameters into the function by calling it, and you will get the result of the function.

3. Expand using mapGetters object

In the component's calculated properties, you can directly use the following method to easily reference the getter property, and then use it just like calling a normal calculated property.

If you want to give a getter property a different name, use the object form:

These knowledge points may be difficult to understand by just reading the documents, but they can be easily digested and understood by manual practice.

Vuex getter parameter passing method

getters: {
    getProductByid: (state) => (id) =>
    {
        return state.productList.find(item => item.id === id);
    }
}

When calling using namespace:

this.$store.getters['yournamespace/getProductByid'](id);

Calling without namespace:

this.$store.getters.getProductByid(id);

Summarize

This is the end of this article about the usage of Getter in vuex. For more relevant Vuex Getter usage 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!

You may also be interested in:
  • Detailed explanation of getters calculation and filtering operations in vuex
  • Detailed explanation of the basic usage of the auxiliary function mapGetters in vuex
  • Talk about the specific usage of Vuex's getters attributes
  • Use of mapState, mapGetters, mapActions, and mapMutations in vuex
  • Additional instructions for using getters and actions in Vuex

<<:  How to solve the problem of automatic package update in Debian system

>>:  MySQL briefly understands how "order by" works

Recommend

Detailed explanation of MySQL master-slave inconsistency and solutions

1. MySQL master-slave asynchrony 1.1 Network Dela...

How to simply encapsulate axios in vue

Inject axios into Vue import axios from 'axio...

MySql 5.7.21 free installation version configuration method under win10

1. Unzip to the location where you want to instal...

Distinguishing between Linux hard links and soft links

In Linux, there are two types of file connections...

MySQL transaction control flow and ACID characteristics

Table of contents 1. ACID Characteristics Transac...

Graphic tutorial on installing CentOS7 on VMware 15.5

1. Create a new virtual machine in VMware 15.5 1....

Summary of CSS sibling element floating analysis

float:left/right/none; 1. Same level floating (1)...

Detailed tutorial on running multiple Springboot with Docker

Docker runs multiple Springboot First: Port mappi...

Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

The following are all performed on my virtual mac...

The difference between the four file extensions .html, .htm, .shtml and .shtm

Many friends who have just started to make web pag...

js to achieve the effect of light switch

This article example shares the specific code of ...

In-depth analysis of Flex layout in CSS3

The Flexbox layout module aims to provide a more ...

How to invert the implementation of a Bezier curve in CSS

First, let’s take a look at a CSS carousel animat...