How to use vuex in Vue project

How to use vuex in Vue project

What is Vuex?

TIP 👉 Official website explanation: Vuex is a state management mode developed specifically for Vue.js applications. It uses centralized storage to manage the status of all components of an application and uses corresponding rules to ensure that the status changes in a predictable manner.

Vuex can be used when multiple pages need to share data. for example:

  • User's personal information management module;
  • From the order checkout page, go to the coupon selection page and select the coupon page. How to save selected coupon information? The state stores the coupon information. When a coupon is selected, mutations are submitted. On the order settlement page, the selected coupon is obtained and the order discount information is updated.
  • The shopping cart module calls the interface to obtain the number of shopping carts each time. The effect is achieved, but each HTTP request consumes browser performance.
  • In my order module, the order list also clicks to cancel the order, and then updates the corresponding order list. In this case, Vuex is also used to store a state, monitor this state, and update the corresponding list when it changes;

The basic idea behind Vuex is borrowed from Flux and Redux. Unlike other modes, Vuex is a state management library designed specifically for Vue to utilize Vue.js's fine-grained data response mechanism for efficient state updates.

Vuex usage cycle diagram

My store directory

  • Modules are used to store different modules
  • action-types.js is for easy management, string mapping, and standardized management
  • mutation-types.js is also for easy management. Just imagine how bad it would be if a bunch of functional modules were mixed together.
  • index.js

Implementing a vuex example

Let's create these files

action-types.js

// Get user information export const QUERY_USER_INFO = "QUERY_USER_INFO"

mutation-types.js

// Set user information export const SET_USER_INFO = 'SET_USER_INFO'

Create a base.js file under modules

base.js

import { QUERY_USER_INFO } from '../action-types'
import { SET_USER_INFO, SET_CUR_MENU_ID } from '../mutation-types'
import api from '@/assets/js/api.js'

// Create state
const state = {
    // User information userInfo: {},
}

// Get data asynchronously, commit to mutations, mutations change state
const actions = {
    /* Get user information */
    [QUERY_USER_INFO] ({ commit }, params) {
        return api.get({
                url: '/system/getUser',
            }, params.vm).then(data => {
                commit(SET_USER_INFO, data)
                return data
        })

    }

}

const getters = {
    // Current user information userInfo: state => state.userInfo
}

// Get synchronously const mutations = {
    [SET_USER_INFO] (state, data) {
        state.userInfo = data
    }
}


export default {
    state,
    actions,
    getters,
    mutations
}

index.js

import Vue from "vue"
import Vuex from "vuex"
import base from "./modules/base.js"

Vue.use(Vuex);

export default new Vuex.Store({
    modules:
        base
    }
})

Header.vue

<span>{{$store.getters.userInfo.name}}</span>

main.js

import Vue from 'vue'
import store from './store'
import { QUERY_USER_INFO } from '@/store/action-types.js'

store.dispatch(QUERY_USER_INFO, {}).finally(() => {
    new Vue({
        router: router(store),
        store,
        render: h => h(App)
    }).$mount('#app')
})

Summarize

This is the end of this article about how to use vuex in Vue projects. For more relevant content about using vuex in Vue projects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Getting Started Tutorial on How to Use Vuex
  • Still using vuex? Learn about pinia
  • Detailed tutorial on using actions in Vuex
  • VueX installation and usage basic tutorial
  • Detailed explanation of the use of Vuex in Vue learning
  • Detailed explanation of the properties and functions of Vuex
  • Detailed explanation of the core concepts and basic usage of Vuex
  • Detailed explanation of Vuex overall case
  • In-depth understanding of the role of Vuex
  • Understanding Vuex in one article
  • Vuex detailed introduction and usage

<<:  Example of how to deploy MySQL 8.0 using Docker

>>:  How to optimize MySQL group by statement

Recommend

Introduction to using the MySQL mysqladmin client

Table of contents 1. Check the status of the serv...

Detailed example of mysql similar to oracle rownum writing

Rownum is a unique way of writing in Oracle. In O...

Detailed explanation of Vue plugin

Summarize This article ends here. I hope it can b...

Summary of new usage of vi (vim) under Linux

I have used the vi editor for several years, but ...

Docker installation and deployment example on Linux

After reading the following article, you can depl...

WeChat Mini Program to Implement Electronic Signature

This article shares the specific code for impleme...

How to install Element UI and use vector graphics in vue3.0

Here we only focus on the installation and use of...

CSS injection knowledge summary

Modern browsers no longer allow JavaScript to be ...

How to implement Vue binding class and binding inline style

Table of contents Binding Class Binding inline st...

Example of automatic import method of vue3.0 common components

1. Prerequisites We use the require.context metho...

Detailed explanation of the basic usage of the img image tag in HTML/XHTML

The image tag is used to display an image in a we...

26 Commonly Forgotten CSS Tips

This is a collection of commonly used but easily ...

Several scenarios for using the Nginx Rewrite module

Application scenario 1: Domain name-based redirec...

JavaScript function detailed introduction

Any number of statements can be encapsulated thro...