Additional instructions for using getters and actions in Vuex

Additional instructions for using getters and actions in Vuex

Preliminary Notes

1.Differences between Vue2.x and Vue3.x:

  • In Vue 3.x, there are no helper functions.
  • There is no difference in other usage of Vuex.

2. Here we only expand and supplement the use of several attributes of Vuex.

Getters added

When getters are written in a submodule, the method in the getters attribute has a total of 4 parameters

getters: {
	/**
	  * Parameter description:
	  * state: represents the satate in the current module
	  * getters: represents the getters object in the current module, generally other methods of the same level * rootState: represents the satate object of the main module * rootGetters represents the getters object of the main module *
	  * The main module is - index.js */  
	getName(state, getters, rootState, rootGetters){
		// Instructions // State and getters can be called directly // rootState.Module name.Attribute name // rootGetters['module name/getters method name under this submodule']
		//Except for state, all other functions use [''], which complies with the naming convention },
	.......
}

Actions added

When actions are written in a submodule, the first parameter context object in the actions method will have 6 objects (there are other properties, but only these 6 are provided for developers to use)

definition

// actions in submodules
actions: {
	/**
	  * Parameter 1: context is an object. If the current actions are defined in a submodule, 
	  * The context will have the following 6 objects for developers to use * 
	  * 1. commit: call mutations
	  * (1). This module calls: commit('this module mutations method name', actual parameter)
	  * (2). Other modules call: commit('module name/other module's mutation method name', actual parameter, {root: true}),
	  * {root:true} fixed parameter, means to call it as the main module * 2. state: get the state of the current module
	  * 3. dispatch: call actions method * (1). This module calls: dispatch('actions method name of this module', actual parameter)
	  * (2). Calls from other modules: dispatch('module name/actions method name of other modules', null, {root: true})
	  * 4. Getters: Get the getters of the current module 
	  * 5. rootState: state under the main module
	  * 6. rootGetters: getters under the main module
	  *
	  * Parameter 2: value is the parameter passed when calling the component */
	refreshUserName(context, value){
	    setTimeout(()=>{
           store.commit('mutations method name', actual parameter value)  
       },2000)
	}
}

Call (Vue3.x)

import { useStore } from 'vuex'
setup(){
    const store = useStore()
    // store === this.$store
    store.dispatch('module name/actions method name', parameter value)
}

During development, if you want to use the above 6 objects, you must obtain the required objects through context. You can directly obtain them through deconstruction

The following is an example from the official website:

The above is the detailed content of the supplementary instructions for the use of getters and actions in Vuex. For more supplementary information on the use of getters and actions in Vuex, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Requesting data instances in vuex actions in vue
  • Detailed example of vuex actions asynchronously modifying state
  • Specific use of Vuex's actions attribute
  • Detailed explanation of the usage of Actions in vuex learning
  • Detailed tutorial on using actions in Vuex

<<:  Introduction and analysis of three Binlog formats in MySQL

>>:  Analysis of parameter transfer process of driver module in Linux

Recommend

Share 10 of the latest web front-end frameworks (translation)

In the world of web development, frameworks are ve...

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

Basic learning and experience sharing of MySQL transactions

A transaction is a logical group of operations. E...

How to implement nginx smooth restart

1. Background During the server development proce...

The homepage design best reflects the level of the web designer

In the many projects I have worked on, there is b...

How to build lnmp environment in docker

Create a project directory mkdir php Create the f...

How to set a dotted border in html

Use CSS styles and HTML tag elements In order to ...

MySQL Series 14 MySQL High Availability Implementation

1. MHA ​By monitoring the master node, automatic ...

JavaScript and JQuery Framework Basics Tutorial

Table of contents 1. JS Object DOM –1, Function –...

Analysis of the principles of docker containers

Table of contents 01 What is the essence of a con...

Fixed a bug caused by scrollbar occupying space

background This bug was caused by滾動條占據空間. I check...

Implementation of Nginx configuration https

Table of contents 1: Prepare https certificate 2:...

Vue realizes the card flip effect

This article example shares the specific code of ...

Summary and examples of vue3 component communication methods

The communication modes of vue3 components are as...

Implementation of nginx proxy port 80 to port 443

The nginx.conf configuration file is as follows u...