参考
https://blog.csdn.net/ProgramarQin/article/details/80357910
https://blog.csdn.net/weixin_44582045/article/details/121852631

this.$store.commit('方法名',值)【存储】

this.$store.state.方法名【取值】

我使用的是 vue-element-admin

1 src/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,
  state: {               // SET_IMPORT 设置的这
    imports: []
  },
  mutations: {
    SET_IMPORT(state, routes) {      // 设置 SET_IMPORT方法,方法中设置存储某个值
      state.imports = routes
    }
  }

})

export default store

2 src/store/getter.js

const getters = {
  sidebar: state => state.app.sidebar,
  errorLogs: state => state.errorLog.logs,
  imports: state => state.imports    // 存储上面设置的 imports 字段
}
export default getters

3 使用 store 存取数据

const nullim = []
    var myMap = {}
    // 塞入键值对,当前上传文件
    myMap['filename'] = rawFile.name
    myMap['result'] = 0
    nullim.push(myMap)
this.$store.commit('SET_IMPORT', nullim)    // 存入缓存,调用 SET_IMPORT 方法


const old_imports = this.$store.getters.imports        // 取缓存中数据,通过字段名
Logo

前往低代码交流专区

更多推荐