What is Vuex?
Vuex can be used when multiple pages need to share data. for example:
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 diagramMy store directory
Implementing a vuex exampleLet'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') }) SummarizeThis 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:
|
<<: Example of how to deploy MySQL 8.0 using Docker
>>: How to optimize MySQL group by statement
Table of contents 1. Check the status of the serv...
Rownum is a unique way of writing in Oracle. In O...
Summarize This article ends here. I hope it can b...
I have used the vi editor for several years, but ...
After reading the following article, you can depl...
This article shares the specific code for impleme...
Here we only focus on the installation and use of...
Modern browsers no longer allow JavaScript to be ...
Table of contents Binding Class Binding inline st...
1. Prerequisites We use the require.context metho...
Important data must be backed up, and must be bac...
The image tag is used to display an image in a we...
This is a collection of commonly used but easily ...
Application scenario 1: Domain name-based redirec...
Any number of statements can be encapsulated thro...