Home>Article>Web Front-end> Easy to understand! Detailed explanation of VUEX state warehouse management
Vuex is a state management pattern developed specifically for Vue.js applications. It uses centralized storage to manage the state of all components of the application, and uses corresponding rules to ensure that the state changes in a predictable way. To put it simply: when the application encounters multiple components sharing state, use vuex.
Scenario: multiple components share data or pass it across components Data time
What are the advantages and disadvantages?
The main advantage is that data and methods can be shared globally. Convenient for unified management
Disadvantage: state variables will be restored and cleared after the page is refreshed, and will not be stored persistently like cookies.
How to solve the loss of vuex state data after the page is refreshed?
Let’s first talk about why it is lost?
Because the data in the store is stored in the running memory, when the page is refreshed, the page will reload the vue instance, and the data in the store will be reassigned.
How to avoid it?
In fact, it mainly depends on the usage scenario. If you want to retain certain data persistently, you can also use cookies or localStorage. For example, some login information, etc.
For example, after requesting the login information, you can first store it in localStorage, bind the variable value in the state to the one in the sessionStorage, and modify the state and localStorage at the same time when modifying the mutations. The final page uses variables in vuex directly. [Related recommendations:vue.js video tutorial]
vuex
Open the terminal and enter the command line npm install vuex - -save to download vuex
Create a new store folder here, create a js named index.js,
In the index, by changing the state ,mutations, actions, and getters are introduced into the ,store and expose the store object.
The following is the code of index.js
/* vuex最核心的管理对象 store */import Vue from 'vue';import Vuex from 'vuex'; // 分别引入这四个文件 这四个文件的内容和用法在下面分别讲到import state from './state';import mutations from './mutations';import actions from './actions';import getters from './getters'; //声明使用插件Vue.use(Vuex)//new 一个Vuex的对象,将state,mutation,action,getters配置到vuex的store中,方便管理数据export default new Vuex.Store({ state, mutations, actions, getters,})
Mount the store to the vue instance
In main.js
import store from './store'// ..........new Vue({ el: '#app', router, store, // *** render: h => h(App)})
We usually put the shared data that needs to be managed into the state, making it look like a global variable, and introduce the state data to the required components.
const state = { userId: '', token: '', name: '', avatar: '', introduction: '', roles: [], tenantId: 1, userInfo: null};
mutations are used to change the state logic in the state, and change the state data in the state synchronously.
What you need to know is that the state object can only be modified through mutations in vuex.
You can modify the state by getting the data obtained from actions, or you can directly define methods in the mutations module to change the state data.
const mutations = { SET_TOKEN: (state, token) => { state.token = token; }, SET_USERID: (state, userId) => { state.userId = userId; }, SET_NAME: (state, name) => { state.name = name; }, SET_ROLES: (state, roles) => { state.roles = roles; }, SET_TENANTID: (state, roles) => { state.tenantId = roles; }, SET_USER_INFO: (state, userInfo) => { state.userInfo = userInfo; }};
Through mutations and the actions module below, you can also see that commit is used to call the mutation module.
The code for calling its mutation module in the component is:
this.$store.commit('SET_TOKEN', token_data)
actions are similar to their mutations, but they can perform asynchronous operations,
and will operate asynchronously The obtained data is submitted to mutations, causing mutations to change the state data in the state. This is often used to obtain data in ajax requests (because it is asynchronous), and commit the obtained data to mutations to update the state data status. The difference between
and mutations is that
- Action submits a mutation instead of directly changing the state.
- Action can contain any asynchronous operation.
For example
/* 下面就是通过actions执行异步Ajax请求, 得到数据后, 通过commit的方法调用mutations 从而更新数据 例如: commit('SET_TOKEN', data.uuid); */const actions = { login({ commit }, userInfo) { // 用户登录 const params = userInfo; params.userName = userInfo.userName.trim() return new Promise((resolve, reject) => { getLogin(params) .then((response) => { const { status, message, data } = response || {}; if (status === 200) { // 存入 参数: 1.调用的值 2.所要存入的数据 commit('SET_USER_INFO', data); commit('SET_TOKEN', data.uuid); commit('SET_USERID', data.id); commit('SET_ROLES', data.roles); commit('SET_NAME', data.realName); commit('SET_TENANTID', data.tenantId || 1); setToken(data.uuid); db.save('userInfo', data); db.save('tenantId', data.tenantId || 1); localStorage.setItem('loginToken', data.uuid); resolve(data); } else { // ElementUI.Message.error(message); // axios拦截统一提示了 } }) .catch((error) => { // ElementUI.Message.error(error.message); // axios拦截统一提示了 reject(error); }); }); },}
The calling method of this actions in the component is:
this.$store.dispatch('user/login', postUser) .then(res => { // ............. })// 我这里的login方法写在了user.js这个module里 所以这里调用是 user/login// 下面会讲到module
Getters are quite For computed properties, it is used to process state data. It has two default parameters. The first default parameter is state, and the second default parameter is getters.
const getters={ plusCount(state){ return state.count + 1 }, //获取state中状态数据对象,和获取getters模块中plusCount数据 totalCount(state,getters){ return getters.plusCount + state.count }}
The code snippet for calling this method in the component is:
this.$store.getters.totalCount()
Read from thestore
instance The simplest way to state is to return a certain state in a calculated property. SinceVuex
's state storage is responsive, wheneverstore.state.count
changes here , the calculated attributes will be re-calculated and updated responsively.
computed: { count: function(){ return this.$store.state.count } },
那么对于以上的store我们就简单介绍完了,相信大家看完后对于vuex会有一定的理解。那么这个时候我们要想,是不是使用this.$store.state
或this.$store.getters.xxx
感到麻烦呢?下面我们介绍另一种引入state和getters的方式
对于上述的在组件中引用state和getters的方法是不是感到麻烦呢?使用mapState你将会感受到便利。
组件中这样使用
//首先我们需要先将辅助函数引入import { mapGetters,mapState } from 'vuex' export default { computed: { // 使用对象展开运算符将 getter 混入 computed 对象中 ...mapGetters( ['plusCount','totalCount'] ) // 使用对象展开运算符将 state 混入 computed 对象中 ...mapState( ['userInfo','count'] ) },methods:{ getData(){ // 这里就能直接使用了 直接使用state 和getters里的数据 // this.userInfo // this.plusCount }}}
Module子模块化管理
store文件夹下的index.js代码如下
import Vue from 'vue'import Vuex from 'vuex'import getters from './getters'Vue.use(Vuex)const modulesFiles = require.context('./modules', true, /\.js$/)const modules = modulesFiles.keys().reduce((modules, modulePath) => { const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules}, {})const store = new Vuex.Store({ modules, getters})export default store
文件目录如图
举例 api.js
import { getKey, getLogin, logout, getInfo } from '@/api/user';import { setToken, removeToken } from '@/utils/auth';import db from '@/utils/localstorage';import router, { resetRouter } from '@/router';import ElementUI from 'element-ui';const state = { userId: '', token: '', name: '', avatar: '', introduction: '', roles: [], tenantId: 1, userInfo: null // roles: ['9999']};const mutations = { SET_TOKEN: (state, token) => { state.token = token; }, SET_USERID: (state, userId) => { state.userId = userId; }, SET_NAME: (state, name) => { state.name = name; }, SET_ROLES: (state, roles) => { state.roles = roles; }, SET_TENANTID: (state, roles) => { state.tenantId = roles; }, SET_USER_INFO: (state, userInfo) => { state.userInfo = userInfo; }};const actions = { // 获取密钥 getKey({ commit }) { return new Promise((resolve, reject) => { getKey() .then((response) => { resolve(response); }) .catch((error) => { reject(error); }); }); }, // 用户登录 login({ commit }, userInfo) { // const { username, password } = userInfo; const params = userInfo; params.userName = userInfo.userName.trim() return new Promise((resolve, reject) => { // console.log(username, password); // setToken(state.token) // localStorage.setItem('loginToken', state.token) getLogin(params) // getLogin({ userName: username.trim(), password: password }) .then((response) => { const { status, message, data } = response || {}; if (status === 200) { // 存入 参数: 1.调用的值 2.所要存入的数据 commit('SET_USER_INFO', data); commit('SET_TOKEN', data.uuid); commit('SET_USERID', data.id); commit('SET_ROLES', data.roles); commit('SET_NAME', data.realName); commit('SET_TENANTID', data.tenantId || 1); setToken(data.uuid); db.save('userInfo', data); db.save('tenantId', data.tenantId || 1); localStorage.setItem('loginToken', data.uuid); resolve(data); } else { // ElementUI.Message.error(message); // axios拦截统一提示了 } }) .catch((error) => { // ElementUI.Message.error(error.message); // axios拦截统一提示了 reject(error); }); }); }, // 获取用户信息 getInfo({ commit, state }) { return new Promise((resolve, reject) => { getInfo(state.token) .then((response) => { const { data } = response; data.roles = response.data.rights.map(String); if (!data) { reject('验证失败,请重新登录。'); } const loginMessage = { memberId: data.id, userName: data.name, userTel: data.mobile, realName: data.realName, incorCom: data.incorCom, virtualCor: data.virtualCor, deptId: data.deptId, deptpath: data.deptpath, deptName: data.deptName }; localStorage.setItem('loginMessage', JSON.stringify(loginMessage)); const { id, roles, realName } = data; // 角色必须是非空数组! if (!roles || roles.length <= 0) { reject('getInfo: 角色必须是非空数组!'); } commit('SET_USERID', id); commit('SET_ROLES', roles); commit('SET_NAME', realName); localStorage.setItem('userRights', roles); // commit('SET_AVATAR', avatar) // commit('SET_INTRODUCTION', introduction) resolve(data); }) .catch((error) => { reject(error); }); }); }, // 用户登出 logout({ commit, state }) { return new Promise((resolve, reject) => { logout(state.token) .then(() => { commit('SET_TOKEN', ''); commit('SET_ROLES', []); db.remove('router'); removeToken(); resetRouter(); resolve(); }) .catch((error) => { reject(error); }); }); }, // 删除token resetToken({ commit }) { return new Promise((resolve) => { commit('SET_TOKEN', ''); commit('SET_ROLES', []); removeToken(); resolve(); }); }, // 动态修改权限 changeRoles({ commit, dispatch }, role) { return new Promise(async(resolve) => { const token = role + '-token'; commit('SET_TOKEN', token); setToken(token); const { roles } = await dispatch('getInfo'); console.log(roles, 'rolesrolesroles'); resetRouter(); // 根据角色生成可访问路由映射 const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true }); // 动态添加可访问路由 router.addRoutes(accessRoutes); // 重置已访问视图和缓存视图 dispatch('tagsView/delAllViews', null, { root: true }); resolve(); }); }};export default { namespaced: true, state, mutations, actions};
这样后可以按功能分module使用
页面中调用就是
// 使用mutationsthis.$store.commit('api/SET_T', keys);// 使用actionsthis.$store.dispatch('user/login', postUser).then(res => {})// 如果没有分module // 那就是 this.$store.commit('SET_T', keys);// 直接调用方法
写完自己也感觉好简单噢(⊙-⊙)
不明白的童鞋在评论区留言咯 ヾ(•ω•`)o
The above is the detailed content of Easy to understand! Detailed explanation of VUEX state warehouse management. For more information, please follow other related articles on the PHP Chinese website!