【问题标题】:Angular 2: Implementing http service for deleteAngular 2:为删除实现 http 服务
【发布时间】:2017-05-11 21:14:32
【问题描述】:

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

类型参数“T”的类型参数不能从 用法。考虑明确指定类型参数。类型 参数候选“响应”不是有效的类型参数,因为它 不是候选“响应”的超类型。属性“类型”的类型 不兼容。类型“字符串”不可分配给类型 '响应类型'。

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

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')); 
  }
}

我做错了什么?

【问题讨论】:

    标签: angularjs http service


    【解决方案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());
     }
    }
    

    【讨论】:

    • 我正在尝试实现这一点,但我仍然不确定当我拨打deleteField()t 应该是什么
    • 这是一个错误。 t 不需要现在你可以试试代码。
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多