vuex

//store->modules->app.js
const state = {
  third: false,
}

const mutations = {
  SET_THIRD: (state, third) => {
    state.third = third
  },
}

const actions = {
   setThird ({ commit }, third) {
    commit('SET_THIRD', third)
   },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

  

//store->getters.js
const getters = {
  third: state => state.app.third,
}
export default getters

  

//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

  

//使用
获取值:
this.$store.getters.third或者...mapGetters(["third"])
mutations设置值:
this.$store.commit('app/SET_THIRD', true)
actions设置值:
this.$store.dispatch('app/setThird', true)

  

  

上一篇:leetcode——18.四数之和


下一篇:Spring Boot系列之多数据源配置