vuex.js 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * 操作全局Vuex。
  3. * 作者:tmzdy
  4. * 时间:‎2021‎年‎10‎月‎14‎日
  5. * 联系:zhongjihan@sina.com
  6. *
  7. */
  8. class vuex {
  9. constructor(store) {
  10. this.store = store;
  11. }
  12. //链式调用
  13. state(){
  14. return this.store.state;
  15. }
  16. //链式调用
  17. getters(){
  18. let t = this;
  19. const g = this.store.getters
  20. let keys = Object.keys(g);
  21. let k = keys.map((el,index)=>{
  22. let f = el.split('/');
  23. let tst = {}
  24. if(f.length==1){
  25. tst[el]=g[el]
  26. }else{
  27. tst[f[0]]={}
  28. tst[f[0]][f[1]] = g[el]
  29. }
  30. return tst
  31. })
  32. let rulst = {};
  33. k.forEach(el=>{
  34. rulst = {...rulst,...el}
  35. })
  36. return rulst;
  37. }
  38. commit(funName,arg){
  39. try{
  40. this.store.commit(funName,arg);
  41. }catch(e){
  42. console.error("未发现函数方法:"+funName)
  43. }
  44. }
  45. actions(funName,arg){
  46. try{
  47. return this.store.dispatch(funName,arg);
  48. }catch(e){
  49. console.error("未发现函数方法:"+funName)
  50. }
  51. }
  52. //获得原始vuex对象。
  53. getVuex(){
  54. return this.store;
  55. }
  56. }
  57. export default vuex;