Home > Web Front-end > Vue.js > Learn more about State and Getters in Vuex

Learn more about State and Getters in Vuex

青灯夜游
Release: 2021-11-24 19:56:02
forward
1740 people have browsed it

The internal organs of Vuex are composed of five parts: State, Getter, Mutation, Action and Module. This article will first give you an in-depth understanding of State and Getter in Vuex. I hope it will be helpful to you!

Learn more about State and Getters in Vuex

Vuex_State

Vuex is a state management tool for vue, in order to make it more convenient for multiple components to share state. [Related recommendations: "vue.js Tutorial"]

Installation

npm install vuex --save复制代码
Copy after login

Use

import Vue from 'vue';import Vuex from 'vuex';

Vue.use(Vuex);const store = new Vuex.Store({  state: {    count: 0
  }
})new Vue({
  store,
})
Copy after login

State

Single state tree, Use a single object to contain all application-level state.

Get Vuex state in Vue component

Vuex provides a mechanism to "inject" state from the component into each sub-component through the store option (call Vue.use(Vuex )).

By registering the store option in the root instance, the store instance will be injected into all sub-components under the root component, and the sub-components can be accessed through this.$store.

<div class="home">
  {{ $store.state.count }}</div>复制代码
Copy after login

mapState Auxiliary Function

When a component needs to obtain multiple states, declaring these states as computed properties will be somewhat repetitive and redundant. In order to solve this problem, we can use the mapState helper function to help us generate calculated properties:

import { mapState } from &#39;vuex&#39;;

computed: {
  ...mapState([&#39;count&#39;]),
},
Copy after login

Use a different name:

computed: {
  ...mapState({
    storeCount: state => state.count,
     // 简写
    storeCount: &#39;count&#39;,  // 等同于 state => state.count
  }),
},
Copy after login

Vuex_Getter

store the calculated properties. The return value of the getter will be cached according to its dependencies, and will only be recalculated when its dependency values ​​change.

Getter receives state as its first parameter and getters as its second parameter.

getters: {
  doubleCount (state) {    return state.count * 2;
  }
}
Copy after login

Accessed through properties

The Getter will be exposed as a store.getters object: this.$store.getters.doubleCount

Accessed through methods

You can also let the getter return a function to pass parameters to the getter

getters: {
  addCount: state => num => state.count + num;
}
Copy after login
this.$store.addCount(3);
Copy after login

mapGetters auxiliary function

import { mapsGetters } from &#39;vuex&#39;;

export default {
  computed: {
    ...mapGetters([
      &#39;doubleCount&#39;,
      &#39;addCount&#39;,
    ])
  }
}
Copy after login

If you want to give a getter attribute another name, use the object form :

mapGetters({
   // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount` 
  storeDoubleCount: &#39;doubleCount&#39;
})
Copy after login

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Learn more about State and Getters in Vuex. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template