action.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {
  2. stDev,
  3. stProd,
  4. proPath
  5. } from '@/config/config.js';
  6. import axios from '@/config/axios.js';
  7. /*
  8. * Action 类似于 mutation,不同在于:
  9. * Action 提交的是 mutation,而不是直接变更状态。
  10. * Action 可以包含任意异步操作。
  11. *
  12. * Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,
  13. * 因此你可以调用 context.commit 提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。
  14. * actions: {
  15. * increment (context) {
  16. * context.commit('increment')
  17. * }
  18. * }
  19. * 我们会经常会用到 ES2015 的 参数解构 来简化代码(特别是我们需要调用 commit 很多次的时候):
  20. * actions: {
  21. * increment ({ commit }) { //{ dispatch, commit, state, getters }
  22. * commit('increment')
  23. * }
  24. * }
  25. *
  26. * Action 通过 store.dispatch 方法触发:
  27. * store.dispatch('increment')
  28. *
  29. * */
  30. export default {
  31. apolloUser ({
  32. commit,
  33. state
  34. }) {
  35. let res = axios.get(proPath + 'users?keyword');
  36. res.then(res => {
  37. // 处理路由
  38. commit('addUser', res);
  39. });
  40. },
  41. // 获取部门列表
  42. apolloOrg ({
  43. commit,
  44. state
  45. }) {
  46. let res = axios.get(proPath + 'organizations');
  47. res.then(res => {
  48. // 处理路由
  49. commit('addOrg', res);
  50. });
  51. }
  52. }