例如,我想做这样的事情:
{hostURL}/api/entities/14/15/16/17
反过来会带回对应ID的所有数据。这种方法行不通。我也试过了,但是也没用:
{hostURL}/api/entities?id=16&id=17
文档显示了如何使用一个ID(在HTTP请求处理下)使用它:https://github.com/angular/in-memory-web-api
有什么办法可以传递多个ID?
谢谢。
答案 0 :(得分:0)
尝试将数组作为ID传递
let IDs = [16, 17];
{hostURL}/api/entities?ids={IDs}
也许这对您有用。
答案 1 :(得分:0)
围绕它。我在angular-in-memory-web-api文档中找到了这个GitHub文件:https://github.com/angular/in-memory-web-api/blob/master/src/app/hero-in-mem-data-override.service.ts,并构建了以下解决方案:
import { getStatusText, STATUS } from 'angular-in-memory-web-api/http-status-codes';
import { InMemoryDbService, RequestInfo } from 'angular-in-memory-web-api';
get(reqInfo: RequestInfo) {
// Extract ids from URL
const ids: Array<number> = reqInfo.req.urlWithParams.match(/[0-9]+/g).map(n => +(n));
// If there's more than one ID in the URL then we call the appropriate function
if (ids.length > 1) {
return this.getRelationshipDetails(reqInfo, ids)
}
}
private getRelationshipDetails(reqInfo: RequestInfo, ids: Array<number>) {
console.log('HTTP GET override')
const entities = reqInfo.collection;
return reqInfo.utils.createResponse$(() => {
const data = entities.filter(entity => ids.indexOf(entity.id) !== -1)
const dataEncapsulation = reqInfo.utils.getConfig().dataEncapsulation;
const options: any = data ?
{
body: dataEncapsulation ? { data } : data,
status: STATUS.OK
} :
{
body: { error: `Entities with ids='${ids}' not found` },
status: STATUS.NOT_FOUND
};
return this.finishOptions(options, reqInfo)
});
}
private finishOptions(options: any, { headers, url }: RequestInfo) {
options.statusText = getStatusText(options.status);
options.headers = headers;
options.url = url;
return options;
}