【问题标题】:React restClient admin-on-restReact restClient admin-on-rest
【发布时间】:2017-02-19 05:21:25
【问题描述】:

admin-on-rest 允许通过编写自定义休息客户端来使用任何 JSON 响应。文档中的示例用于使用来自 json-server project 的 JSON,这很简单。

我想知道在 admin-on-rest 中使用 this api 有多么容易,只需对 restClient 进行细微更改。

【问题讨论】:

  • 您的链接似乎指向错误页面
  • 是的,即使对我来说链接也不起作用。请删除您发布的有问题的链接
  • 他们发布了带有更新文档的 0.4.0 版本。我更新了链接并修复了显示链接的语法问题

标签: json rest reactjs admin-on-rest


【解决方案1】:

好的 - 让我们看看 admin on rest 的来源(文件 admin-on-rest/src/util/fetch.js),我们对 fetchJson 方法很感兴趣。

该方法返回 fetch promise,它尝试在该代码中解析 json:

try {
  json = JSON.parse(body);
} catch (e) {
  // not json, no big deal
}

然后它返回:return { status, headers, body, json };
但是我们在结果中有 body 并且可以重用它,或者我们可以在 json 中使用已解析的对象

对于您的示例,我们可能会这样做(缺少一些代码):

const httpClient = (url, options = {}) => {
    if (!options.headers) {
        options.headers = new Headers({ Accept: 'application/json' });
    }
    options.withCredentials = true;
        return fetchUtils.fetchJson(url, options).then(({status, headers, body, json}) => {
        json = json['results'] ? json['results'] : json;
        return {status, headers, body, json};
    });
}

所以我们只是在该行的架构中通过集合中的“结果”覆盖了 json 对象:
json = json['results'] ? json['results'] : json;
现在您可以在 Admin 中使用该客户端

<Admin restClient={restClient}>
...
</Admin>

警告!!!这将影响管理员的所有请求。但是您可以添加其他参数。如果您不想使用json = json['results'] ? json['results'] : json;,您可以添加其他参数或签入方法 fetch

【讨论】:

    【解决方案2】:

    如果我没记错的话,你想从(例如)重写 url: https://marmelab.com/admin-on-rest-demo/#/customers/684 到: https://marmelab.com/admin-on-rest-demo/?customers=684

    你可以重写restClient.js中的GET_ONE: 案例 GET_ONE: 网址 = ${apiUrl}/${resource}/${params.id}; 到: 案例 GET_ONE: 网址 = ${apiUrl}/?${resource}=${params.id};

    这将通过参数而不是 url 部分来检索记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      相关资源
      最近更新 更多