$u.mixin.js 563 B

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