Vuex是什么
Vuex是为Vue.js提供的状态管理模式/工具。
官方文档
https://vuex.vuejs.org/zh/api/#replacestate
描述
当多个组件共享状态时,会出现1.多个视图依赖同一个状态 2.不同视图的行为会改变同一个状态。这种复杂庞大的系统如果采用props和emit事件的方式进行父子组件传值会导致代码很难维护,兄弟组件的传值更是不方便(bus中央事件总线和父组件中转)
将共享状态抽离,进行集中管理,我们的组件树构成一个巨大的‘视图’,无论树的哪个位置,都能获取状态或触发行为改变状态。这就回到了Vuex。
原理
核心内容
| 1
 | state,getter,mutation,action, module(模块)
 | 
安装
| 1
 | cnpm install vuex --save
 | 
引入
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | import Vue from 'vue'
 import Vuex from 'vuex'
 Vue.use(Vuex)
 const store = new Vuex.Store({
 state,
 getters,
 mutations,
 actions
 })
 export default store
 
 
 import store from 'store.js'
 new Vue({
 store
 。。。
 })
 
 
 | 
使用
集中时的使用
| 12
 3
 4
 5
 6
 
 | const state = {
 name: '状态'
 }
 
 this.$store.state.name
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | const mutations = {
 changeName(state, payload) {
 state.name = payload || '改变状态'
 }
 }
 
 this.$store.commit('changeName')
 this.$store.commit('changeName','payload')
 this.$store.commit({
 type: 'changeName',
 payload: '此时mutation里接收的第二个参数payload是整个对象'
 })
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | 
 const actions = {
 commitMutations(context, payload) {
 
 
 let name = context.state.name
 
 context.commit('changeName')
 }
 }
 
 this.$store.dispatch('commitMutations')
 this.$store.dispatch('commitMutations','payload')
 this.$store.dispatch({
 type: 'commitMutations',
 payload: '此时actions里接收的第二个参数payload是整个对象'
 })
 
 | 
| 12
 3
 4
 5
 6
 
 | const getters = {
 newName: state => state.name+1
 }
 
 this.$store.getters.newName
 
 | 
模块时的使用
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | const state = {name: 'module'}
 export default {
 namespaced: true,
 state
 }
 
 
 const module = require.context('./module', true, /\.js$/)
 let obj = {}
 module.keys().forEach(item => {
 const keys = item.replace(/\.\/|\.js/g, '')
 let last = keys.split('/')[keys.split('/').length-1]
 obj[last] = module[item].default
 })
 
 let store = new Vuex.Store({
 modules: {
 ...obj
 }
 })
 
 
 this.$store.state['module'].name
 this.$store.commit('module/changeName', {payload:'payload'})
 this.$store.dispatch('module/commitMutations', {payload:'payload'})
 ...mapActions('module', {
 actionsFunc: 'commitMutations'
 })
 this.actionsFunc({payload:'payload'})
 
 | 
vuex辅助函数的使用
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | 
 computed: {
 ...mapState({
 name: 'name'
 }),
 ...mapGetters({
 newName: 'newName'
 })
 },
 methods: {
 ...mapActions([
 'commitMutations'
 ]),
 ...mapMutations([
 'changeName'
 ])
 },
 mounted() {
 this.changeName('d')
 this.commitMutations('c')
 
 
 }
 
 | 
mutation-type
一种复杂项目的优化代码可维护性的方式,我暂时不喜欢用
应用
列表封装,每个列表页配置一个状态管理module,由封装的组件去触发
问题
vuex 存储状态 刷新丢失问题 优化,1.vue-along插件,2.localStorage(不建议,小数据干脆直接用localstorage存得了),3.刷新就重新请求
| 12
 3
 4
 
 | window.addEventListener("beforeunload",()=>{this.$store.state.media.openid = 88833888
 localStorage.setItem('stateData', JSON.stringify(this.$store.state))
 })
 
 | 
| 12
 3
 4
 
 | const data = JSON.parse(localStorage.getItem('stateData'))if (data) {
 this.$store.replaceState(Object.assign({}, this.$store.state, data))
 }
 
 |