/* * @Author: 丘骏_jun * @Date: 2019-11-21 10:59:17 * @Last Modified by: 丘骏_jun * @Last Modified time: 2021-05-14 18:28:18 */ import Vue from 'vue' import Vuex from 'vuex' // import fn from '../utils/fn.js' Vue.use(Vuex) const state = {} // 显式更改state,内部只允许同步,调用方式:$store.commit(key, value) const mutations = {} // 相当于computed,调用方式:store.getters.xxx const getters = {} // 允许异步,需通过store.dispath分发,调用store.commit更改state const actions = {} /** * 首字母变大写 * @param {string} name */ function getBigName(name){ return [name[0].toUpperCase(), ...name.substring(1)].join('') } /** * 使用create,快捷创建简易字段 * 通用创建store字段,包含state,getters,mutations * @param {string} key * @param {*} default_value * @param {function} mutation */ function create (key, default_value, mutation) { state[key] = default_value mutations['set' + getBigName(key)] = mutation || ((state, value) => {state[key] = value}) getters['get' + getBigName(key)] = state => state[key] } // 用户信息 create('userData', {}) // danmuData: [], // 弹幕数据 // luckNum: 0, // 幸运值数据 // shareNum: 0, // 分享次数 // leftNum: 0, // 剩余抽奖次数 const store = new Vuex.Store({ state, getters, mutations, actions, }) export default store