$uv.mixin.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * @Author: PoJun
  3. * @Date: 2023-05-14 17:58:20
  4. * @LastEditors: PoJun
  5. * @LastEditTime: 2023-10-20 13:44:40
  6. * @Message: Nothing
  7. */
  8. import { mapState } from "vuex";
  9. import store from "@/store";
  10. // 尝试将用户在根目录中的store/index.js的vuex的state变量,全部加载到全局变量中
  11. let $uStoreKey = [];
  12. try {
  13. $uStoreKey = store.state ? Object.keys(store.state) : [];
  14. } catch (e) {
  15. console.log(e);
  16. }
  17. module.exports = {
  18. created() {
  19. // 将vuex方法挂在到$uv中, 方法的调用不能再页面的时候. 需要等方法挂载完毕
  20. // 使用方法为:如果要修改vuex的state中的user.name变量为"史诗" => this.$uv.vuex('user.name', '史诗')
  21. // 如果要修改vuex的state的version变量为1.0.1 => this.$uv.vuex('version', '1.0.1')
  22. this.$uv.vuex = (name, value) => {
  23. this.$store.commit("$uStore", {
  24. name,
  25. value,
  26. });
  27. };
  28. },
  29. computed: {
  30. // 将vuex的state中的所有变量,解构到全局混入的mixin中
  31. ...mapState($uStoreKey),
  32. },
  33. };