http - angular2 http.delete on-memory-web-api

标签 http angular typescript angular-cli in-memory-database

我的项目使用本地服务器,后端在 .NET Core 上运行,我在 Mac 上开发,所以我无法运行后端的实例。因此,我为使用 in-memory-web-api 的项目创建了一个单独的 (angular-cli) 环境。

我已将其设置好并适用于 get/和 get/:id。但删除不起作用。有什么方法可以使这项工作起作用,或者不能用 i-m-w-a 做到这一点?

服务

public getOperations(): Observable<IOperation[]>{
    return this.http.get(`${environment.baseUrl}operations/`)
        .map(this.sharedService.extractData)
        .catch(this.sharedService.handleError);
  }
  public getOperation(operationId: string):Observable<IOperation>{
    return this.http.get(`${environment.baseUrl}operations/${operationId}`)
        .map(this.sharedService.extractData)
        .catch(this.sharedService.handleError);
  }
  public deleteOperation(operationId: string): Observable<any>{
    return this.http.delete(`${environment.baseUrl}operations/${operationId}`)
        .map(this.sharedService.extractData)
        .catch(this.sharedService.handleError);
  }

内存数据库

export class InMemoryDataService implements InMemoryDbService {
    createDb() {
        let operations: IOperation[] = [
            new Operation({
                id:'001',
                name: 'Operation ABC',
                status:'Active',
                startDate: new Date().toLocaleDateString()
            }),
            new Operation({
                id:'002',
                name: 'Operation DEF',
                status:'Closed',
                startDate: new Date().toLocaleDateString(),
                endDate: new Date().toLocaleDateString()
            }),
            new Operation({
                id:'003',
                name: 'Operation GHI',
                status:'Closed',
                startDate: new Date().toLocaleDateString(),
                endDate: new Date().toLocaleDateString()
            }),
            new Operation({
                id:'004',
                name: 'Operation JKL',
                status:'Closed',
                startDate: new Date().toLocaleDateString(),
                endDate: new Date().toLocaleDateString()
            }),
        ];
        return {operations};
    }
}

忘记添加调用我的服务方法的组件方法:

public deleteOperation(operation: IOperation){
        this.confirmationService.confirm({
            message: `Are you sure that you want to delete '${operation.name}'?`,
            accept: () => {
                this.operationsService.deleteOperation(operation.id)
                    .subscribe(()=>{
                        this.loadOperations();
                    })
            }
        });
    }

最佳答案

好的,我找到问题了, 我从 deleteOperation 服务方法中删除了 .map 函数。 extractData 执行了 res.json(),但它不起作用,因此失败了。

关于http - angular2 http.delete on-memory-web-api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42840290/

相关文章:

http - 从 Arduino 发送适当的 Post 请求

ruby - 如何在 Ruby 中编写内容更改 Web 代理服务器?

angular - 订阅 Angular 2 Http 两次?

ios - 应用程序启动时的路由问题 Ionic 4

typescript - Angularjs 2.0 不加载 templateUrl

node.js - 如何在 Node 中限制 HTTP 请求的带宽?

java - 如果客户端继续监听请求,请检查 servlet API?

javascript - 表单验证失败后如何显示模式?

angularjs - 构建混合 angular/angularjs 应用程序 - 如何在 angularjs 应用程序中加载 Angular 模块

javascript - typescript:使用 get/set 来实现公共(public)功能