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

Step by step guide to build a calendar component with React

Table of contents Business Background Using Techn...

How to quickly install tensorflow environment in Docker

Quickly install the tensorflow environment in Doc...

Detailed explanation of Navicat's slow remote connection to MySQL

The final solution is in the last picture If you ...

Detailed explanation of JavaScript program loop structure

Table of contents Select Structure Loop Structure...

Solution to prevent caching in pages

Solution: Add the following code in <head>: ...

CentOS server security configuration strategy

Recently, the server has been frequently cracked ...

Vue uses mockjs to generate simulated data case details

Table of contents Install mockjs in your project ...

Specific steps for Vue browser to return monitoring

Preface When sharing a page, you hope to click th...

Use of MySQL stress testing tool Mysqlslap

1. MySQL's own stress testing tool Mysqlslap ...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

How to install MySQL 8.0.13 in Alibaba Cloud CentOS 7

1. Download the MySQL installation package (there...

How to install Nginx in CentOS7 and configure automatic startup

1. Download the installation package from the off...

A must-read career plan for web design practitioners

Original article, please indicate the author and ...