其他分享
首页 > 其他分享> > vuex

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)

  

  

标签:const,third,getters,modules,state,vuex,store
来源: https://www.cnblogs.com/chicidol/p/15379653.html