【发布时间】:2017-03-06 16:15:43
【问题描述】:
我正在为 Angular 2 使用 angular-in-memory-web-api。到目前为止,我只使用 GET 调用,它运行良好。
我要调用的 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);
}
还有模拟数据:
let test = [
{ testId: 'e0d05d2b-3ec3-42ae-93bc-9937a665c4d6', missingData: 'qwerty', moreMissingData: 'asdfgh' },
{ testId: 'dccef969-b9cf-410a-9973-77549ec47777', missingData: 'qwerty', moreMissingData: 'asdfgh' },
{ testId: '20716fd7-1f50-4a12-af16-52c009bc42ab', missingData: 'qwerty', moreMissingData: 'asdfgh' }
];
如果我理解正确,这段代码将假定我想创建一些东西,因此将我的 testId 与 id: 1 一起反弹回来(它甚至不遵循我的数据结构)。
那么,我的问题是,如何通过 POST 调用获取我的模拟数据?
【问题讨论】:
-
in-memory-web-api 只是用来玩一点和熟悉框架。如果你想深入学习,你应该实现一个“真实的”假后端。看看这个例子:github.com/cornflourblue/angular2-registration-login-example/…
-
谢谢,这个例子效果很好。
-
看起来是一个很好的例子。最新版本的 Angular 和 RxJS 是否有类似的示例
-
是不是说我们不能伪造在内存中添加一个新数据,然后从内存模块中返回新的数据集?