在Json中编写Json以进行redux操作

时间:2018-02-14 14:47:37

标签: json redux action

我试图通过使用来自不同文件的导入来为redux创建一个动作,但有些东西不起作用。显示代码更容易:

在Api.js:

exopt const API =  {
  GET_LIST: {path: '/list', method: 'GET'},
  POST_LIST: {path: '/users/data', method: 'POST'}
};

在action.js中:

import { API } from './Api';

export const fetchList = () => ({
  type: 'API_ACTION',
  payload: {
    API.GET_LIST
  }
)};

我希望fetchList返回以下操作:

{
  type: 'API_ACTION',
  payload: {
    path: '/exercises/list',
    method: 'GET'
  }
}

但我得到了一个错误:

Syntax error: ... Unexpected token, expected , (7:9)

   5 |   type: 'API_ACTION',
   6 |   payload: {
>  7 |      API.GET_LIST,
     |          ^

我做错了什么?

感谢帮助!

1 个答案:

答案 0 :(得分:0)

您正在尝试在对象上设置键而不指定值。

fetchList中的action.js应该是这样的:

export const fetchList = () => ({
  type: 'API_ACTION',
  payload: {
    path: API.GET_LIST.path,
    method: API.GET_LIST.method,
  },
)};

OR(甚至更简单)

export const fetchList = () => ({
  type: 'API_ACTION',
  payload: API.GET_LIST,
)};

还有许多其他方法可以分配您的有效负载(对象传播等),但上面的内容应该可以帮助您。

相关问题