1. /**
    2. *
    3. * @param {*} actionCreators action创建函数或者对象
    4. * @param {*} dispath store上面的分发方法
    5. */
    6. export default function bindActionCreators(actionCreators, dispath) {
    7. if (typeof actionCreators === 'function') {
    8. getAutoDispatchAction(actionCreators, dispath);
    9. } else if (typeof actionCreators === 'object') {
    10. const autoActionObject = {};
    11. for (const key in actionCreators) {
    12. if (Object.hasOwnProperty.call(actionCreators, key)) {
    13. const action = actionCreators[key];
    14. autoActionObject[key] = getAutoDispatchAction(action, dispath);
    15. }
    16. }
    17. return autoActionObject;
    18. }
    19. else {
    20. throw TypeError('action must be an object or function meas an action')
    21. }
    22. }
    23. function getAutoDispatchAction(action, dispatch) {
    24. return function (...arg) {
    25. const autoAction = action(arg);
    26. dispatch(autoAction);
    27. }
    28. }