$u.mixin.js 800 B

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