PrefaceVuex 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:
1. DescriptionThe 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.
2. Getter returns a functionPassing 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 objectIn 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 methodgetters: { 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); SummarizeThis 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:
|
<<: How to solve the problem of automatic package update in Debian system
>>: MySQL briefly understands how "order by" works
1. MySQL master-slave asynchrony 1.1 Network Dela...
This article mainly introduces the implementation...
Inject axios into Vue import axios from 'axio...
1. Unzip to the location where you want to instal...
In Linux, there are two types of file connections...
Table of contents 1. ACID Characteristics Transac...
1. Create a new virtual machine in VMware 15.5 1....
float:left/right/none; 1. Same level floating (1)...
Docker runs multiple Springboot First: Port mappi...
The following are all performed on my virtual mac...
Optimize the fastcgi configuration file fcgiext.i...
Many friends who have just started to make web pag...
This article example shares the specific code of ...
The Flexbox layout module aims to provide a more ...
First, let’s take a look at a CSS carousel animat...