Angular 2:为删除

时间:2017-05-10 18:29:25

标签: angularjs http service

我正在尝试编写一个服务,用于从我的外部数据库中删除,并且我一直看到这个错误:

  

无法从中推断出类型参数“T”的类型参数   用法。考虑明确指定类型参数。类型   参数候选者'Response'不是有效的类型参数,因为它   不是候选人“回应”的超类型。财产'类型'的类型   是不相容的。类型'字符串'不能分配给类型   '的responseType'。

^并且此错误突出显示该行:

return this.http.delete(`${this.base_url}/${id}`)

这是服务:

import { Injectable } from '@angular/core';
import { CheckIn } from './check-in';
import { Headers, RequestOptions, Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';


@Injectable()

export class CheckInService {

  private base_url = "http://localhost:3000/";

  constructor (private http: Http) {}

  delete(id:string): Observable<CheckIn[]> {

    return this.http.delete(`${this.base_url}/${id}`)
                    .map((res:Response) => res.json()) .json() on the response to return data
                     .catch((error:any) => Observable.throw(error.json().error || 'Server error')); 
  }
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个示例代码。我认为问题在于你接受回应的方式。

主要代码

import { Component, OnInit } from "@angular/core";
import {CheckInService} from "../shared/checkin.service";
@Component({
 selector: "test",
 templateUrl: "./test.html",
 styleUrls: ["./test.css"]
})
export class TestComponent implements OnInit{

dataMap:any;
dataDeleteMap:any;
constructor(public dataMapService:CheckInService) {
    this.dataMapService=dataMapService;
}
ngOnInit(){}

  deleteField(id:string){
    this.dataMapService.deleteField(id).subscribe(jsonData=>{
    this.dataDeleteMap=jsonData;
  }); 
 }
}

<强>服务

import { Injectable ,OnInit,OnDestroy} from '@angular/core';
import { Http, Response,Headers,RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/operator/map';

@Injectable()
export class CheckInService {

data:any;
baseUrl:string='http://localhost:3000/';

constructor(public http:Http) { 
}

 deleteField(id:string):Observable<Response>{
   return this.http.delete(`${this.baseUrl}/${id}`)
   .map(result=>result.json());
 }
}