angular-http -delete on in-memory-web-api

时间:2017-03-16 16:55:43

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

我的项目使用内部服务器,后端运行在.NET核心上,我在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();
                    })
            }
        });
    }

2 个答案:

答案 0 :(得分:1)

好的,我发现了问题, 我从deleteOperation服务方法中删除了.map函数。 extractData做了一个res.json(),它不起作用,因此失败了。

答案 1 :(得分:0)

您必须.subscribe()Observable返回的deleteOperation()才能http执行请求。否则,将不会进行API调用。