$u.mixin.js 771 B

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