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
Table of contents Business Background Using Techn...
Quickly install the tensorflow environment in Doc...
The final solution is in the last picture If you ...
Table of contents Select Structure Loop Structure...
Solution: Add the following code in <head>: ...
Recently, the server has been frequently cracked ...
Table of contents Install mockjs in your project ...
Today, I will answer these newbie questions: Build...
Preface When sharing a page, you hope to click th...
1. MySQL's own stress testing tool Mysqlslap ...
What is a web page? The page displayed after the ...
As shown above, the navigation is fixed at the to...
1. Download the MySQL installation package (there...
1. Download the installation package from the off...
Original article, please indicate the author and ...