状态管理:vuex(适用于vue2)
创始人
2024-05-03 19:04:54
0

1. Vuex的五个核心概念:state、getters、mutations、actions、modules。

2. 名词解释:

  • state:状态数据(各种数据变量)
  • getters:对状态数据经过一些处理后,生成的数据(变量)
  • mutations:同步方法,可用来修改state值。注:方法名常一律都用大写字母表示, 如:SET_NAME(state, payLoad) {...}
  • actions:  异步方法,如通过ajax异步请求后端获取一些数据,然后交给mutations去修改state。
  • modules: 模块化vuex,每一个模块都拥有自己独立的 state、mutation、action、 getters,使得结构非常清晰,方便管理。注:调用时,常用斜杠/符号, 如:模块名/xxx

一、安装对应库

安装vuex库: npm i vuex

二、store状态管理文件

store/modules/app.js文件

import Cookies from 'js-cookie'
const state = {sidebar: {opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,withoutAnimation: false},device: 'desktop',
}const mutations = {OPEN_SIDEBAR: (state) => {Cookies.set('sidebarStatus', 1)state.sidebar.opened = true},CLOSE_SIDEBAR: (state, withoutAnimation) => {Cookies.set('sidebarStatus', 0)state.sidebar.opened = falsestate.sidebar.withoutAnimation = withoutAnimation},TOGGLE_DEVICE: (state, device) => {state.device = device},
}const actions = {closeSideBar({ commit }, { withoutAnimation }) {commit('CLOSE_SIDEBAR', withoutAnimation)},toggleDevice({ commit }, device) {commit('TOGGLE_DEVICE', device)},
}export default {namespaced: true, // 模块化state,mutations,actions
}

store/getters.js文件

// 把不同模块的都汇总
const getters = {sidebar: state => state.app.sidebar, // app模块device: state => state.app.device,token: state => state.user.token, // user模块avatar: state => state.user.avatar,name: state => state.user.name,introduction: state => state.user.introduction,
}
export default getters

store/index.js文件

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'Vue.use(Vuex)// 将不同模块的状态管理汇总
// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {// set './app.js' => 'app'const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')const value = modulesFiles(modulePath)modules[moduleName] = value.defaultreturn modules
}, {})const store = new Vuex.Store({modules,getters
})export default store

三、数据持久化处理(解决:刷新浏览器后,vuex数据消失问题)

工具库:js-cookie、

四、.vue页面:使用状态管理

1. 状态管理:分模块和不分模块,分模块需要使用斜杠/符号来标志(模块名/xxx)。

2. 使用方式(2种):

方式1:  使用vuex库内置的4个mapXX函数,来解构vuex对象

方式2: 使用this.$store.xxx方式(即:将store注册到vue的)。

参考链接:

  • vuex的五种属性及使用方法 - 掘金

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...