其他分享
首页 > 其他分享> > vue $store全局变量设置

vue $store全局变量设置

作者:互联网

store/modules/dict.js

import { getDicts } from '@/api/index'

const dict = {
  state: {
    dicts: [],
  },

  mutations: {
    SET_DICT: (state, data) => {
      state.dicts = data
    },
  },

  actions: {
    getDict({ commit }) {
      return new Promise((resolve, reject) => {
        getDicts().then(res => {
          let data = res.data;
          commit('SET_DICT', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }

}

export default dict

store/getters.js

const getters = {
  dicts: state => state.dict.dicts
}
export default getters

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import dict from './modules/dict'

Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    dict
  },
  getters
})

export default store

permission.js 判断完userinfo之后

store.dispatch('getDict');

访问

this.$store.getters.dicts

 

标签:vue,dicts,getters,state,dict,全局变量,data,store
来源: https://www.cnblogs.com/jqynr/p/16319824.html