Angular2-使用POST和内存中的AngularWeb api

Angular2-使用POST和内存中的AngularWeb api,angular,post,get,Angular,Post,Get,我正在玩angular 2的angular内存web api。到目前为止,我只使用了GET calls,它工作得很好 我将要调用的API只使用POST调用,因此我开始将GET调用重写为POST调用,但随后它们停止返回模拟数据。在下面的测试函数中,我希望通过id获取作为TestResponse对象的数据: postTest(id: string): Promise<TestResponse> { return this.http .post(this.testU

我正在玩angular 2的angular内存web api。到目前为止,我只使用了GET calls,它工作得很好

我将要调用的API只使用POST调用,因此我开始将GET调用重写为POST调用,但随后它们停止返回模拟数据。在下面的测试函数中,我希望通过id获取作为TestResponse对象的数据:

postTest(id: string): Promise<TestResponse> {
    return this.http
        .post(this.testUrl, JSON.stringify({ testId: id }), { headers: this.headers })
        .toPromise()
        .then(response => response.json().data as TestResponse)
        .catch(this.handleError);
}
如果我理解正确,这段代码将假设我想要创建一些东西,并因此将我的testId与id:1(它甚至不遵循我的数据结构)一起反弹回来


因此,我的问题是,如何通过POST调用获取模拟数据?

可以在内存中的数据服务实现中重写HTTP方法

在重写方法(例如POST)中,可以对集合名称作出反应(通过
RequestInfo
参数),以基于每个端点/集合提供特定功能

提供的示例仅处理GET调用:

因此,覆盖POST功能可能如下所示:

import { InMemoryDbService, RequestInfo, STATUS, ResponseOptions } from 'angular-in-memory-web-api';

export class Your InMemoryDataService implements InMemoryDbService {

  // ...

  post(requestInfo: RequestInfo) {
    const collectionName = requestInfo.collectionName;
    if (collectionName === 'somedatatype') {
      // Intercept POST calls to the 'somedatatype' collection:
      // E.g. add some fields to our entity that would be set in the backend,
      const data = requestInfo.utils.getJsonBody(requestInfo.req);
      const collection = requestInfo.collection;
      data.extraField = 'hello';

      // set id to next highest number 
      data.id = collection.map(item => item.id).reduce((cur, next) => cur > next ? cur : next) + 1;

      // ... add the item to the collection
      collection.push(data);

      // forge the response
      const options: ResponseOptions = {
        body: { data  },
        status: STATUS.OK,
        headers: requestInfo.headers,
        url: requestInfo.url
      };

      // use createResponse$ to return proper response
      return requestInfo.utils.createResponse$(() => options);
    }

    // let the default POST handle all other collections by returning undefined
    return undefined; 
  }
}

内存中的WebAPI只是为了玩一玩,熟悉一下框架。如果你想深入学习,你应该实现一个“真”假后端。看看这个例子:谢谢,这个例子效果很好。看起来是个很好的例子。Angular和RxJS的最新版本有类似的例子吗?这是否意味着我们不能在内存中添加新数据,然后从内存模块中返回新数据集?谢谢,这很好。救生员。
import { InMemoryDbService, RequestInfo, STATUS, ResponseOptions } from 'angular-in-memory-web-api';

export class Your InMemoryDataService implements InMemoryDbService {

  // ...

  post(requestInfo: RequestInfo) {
    const collectionName = requestInfo.collectionName;
    if (collectionName === 'somedatatype') {
      // Intercept POST calls to the 'somedatatype' collection:
      // E.g. add some fields to our entity that would be set in the backend,
      const data = requestInfo.utils.getJsonBody(requestInfo.req);
      const collection = requestInfo.collection;
      data.extraField = 'hello';

      // set id to next highest number 
      data.id = collection.map(item => item.id).reduce((cur, next) => cur > next ? cur : next) + 1;

      // ... add the item to the collection
      collection.push(data);

      // forge the response
      const options: ResponseOptions = {
        body: { data  },
        status: STATUS.OK,
        headers: requestInfo.headers,
        url: requestInfo.url
      };

      // use createResponse$ to return proper response
      return requestInfo.utils.createResponse$(() => options);
    }

    // let the default POST handle all other collections by returning undefined
    return undefined; 
  }
}