guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame] | 1 | import { mapState } from 'vuex' |
| 2 | import store from "@/store" |
| 3 | |
| 4 | // 尝试将用户在根目录中的store/index.js的vuex的state变量,全部加载到全局变量中 |
| 5 | let $uStoreKey = []; |
| 6 | try{ |
| 7 | $uStoreKey = store.state ? Object.keys(store.state) : []; |
| 8 | }catch(e){ |
| 9 | |
| 10 | } |
| 11 | |
| 12 | module.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 | } |