jazz
2023-12-28 583e1038163d7b95f7927f83b19d81f6eac32020
提交 | 用户 | age
3ac5f2 1 /*
J 2  * @Author: 丘骏_jun
3  * @Date: 2019-11-21 10:59:17
4  * @Last Modified by: 丘骏_jun
5  * @Last Modified time: 2021-05-14 18:28:18
6  */
7 import Vue from 'vue'
8 import Vuex from 'vuex'
9 // import fn from '../utils/fn.js'
10 Vue.use(Vuex)
11
12 const state = {}
13
14
15 // 显式更改state,内部只允许同步,调用方式:$store.commit(key, value)
16 const mutations = {}
17
18 // 相当于computed,调用方式:store.getters.xxx
19 const getters = {}
20
21 // 允许异步,需通过store.dispath分发,调用store.commit更改state
22 const actions = {}
23
24 /**
25  * 首字母变大写
26  * @param {string} name
27  */
28 function getBigName(name){
29     return [name[0].toUpperCase(), ...name.substring(1)].join('')
30 }
31
32 /**
33  * 使用create,快捷创建简易字段
34  * 通用创建store字段,包含state,getters,mutations
35  * @param {string} key
36  * @param {*} default_value
37  * @param {function} mutation
38  */
39 function create (key, default_value, mutation) {
40     state[key] = default_value
41     mutations['set' + getBigName(key)] = mutation || ((state, value) => {state[key] = value})
42     getters['get' + getBigName(key)] = state => state[key]
43 }
44
45 // 用户信息
46 create('userData', {})
47 // danmuData: [], // 弹幕数据
48 // luckNum: 0, // 幸运值数据
49 // shareNum: 0, // 分享次数
50 // leftNum: 0, // 剩余抽奖次数
51
52 const store = new Vuex.Store({
53     state,
54     getters,
55     mutations,
56     actions,
57 })
58
59 export default store