$u.mixin.js 795 B

123456789101112131415161718192021222324252627
  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. this.$u.vuex = (name, value) => {
  15. this.$store.commit('$uStore', {
  16. name,value
  17. })
  18. }
  19. },
  20. computed: {
  21. // 将vuex的state中的所有变量,解构到全局混入的mixin中
  22. ...mapState($uStoreKey)
  23. }
  24. }