blob: 94ed2eedd00319f20ba43c252ad6574402719503 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001import { mapState } from 'vuex'
2import store from "@/store"
3
4// 尝试将用户在根目录中的store/index.js的vuex的state变量,全部加载到全局变量中
5let $uStoreKey = [];
6try{
7 $uStoreKey = store.state ? Object.keys(store.state) : [];
8}catch(e){
9
10}
11
12module.exports = {
13 created() {
14 // 将vuex方法挂在到$u中
15 // 使用方法为:如果要修改vuex的state中的user.name变量为"史诗" => this.$u.vuex('user.name', '史诗')
16 // 如果要修改vuex的state的version变量为1.0.1 => this.$u.vuex('version', '1.0.1')
17 this.$u.vuex = (name, value) => {
18 this.$store.commit('$uStore', {
19 name,value
20 })
21 }
22 },
23 computed: {
24 // 将vuex的state中的所有变量,解构到全局混入的mixin中
25 ...mapState($uStoreKey)
26 }
27}