簡體   English   中英

不可分配給“可觀察”類型 <task> &#39;。角4

[英]is not assignable to type 'Observable<task>'.Angular 4

我對Observable類型有疑問,知道嗎?

    import { Injectable } from '@angular/core';
    import {HttpClient} from '@angular/common/http'
    import {Observable} from 'rxjs/Observable'
    import {task} from './../models/taks.class'


    @Injectable()
    export class TaskService {
      public API: string ='http://localhost:3000/tasks'

      constructor(public httpservice: HttpClient){}
      getall(): Observable<task[]>{
        return this.httpservice.get(this.API)
      }
      add(task:task): Observable<task> {
        return this.httpservice.post(this.API,{
          title:task.title,
          co:task.co
        })
      }
      edit(task:task):Observable<task>{
        return this.httpservice.put(`${this.API}/${task.id}`, {
          title: task.title,
          co: task.co
      })
    }
      delete(id:number):Observable<task>{
        return this.httpservice.delete(`${this.API}/${id}`)
      }
    }
  1. 在線return this.httpservice.get(this.API)

我得到這個

錯誤:無法分配給“可觀察”類型

我不知道如何解決這個問題

import {Injectable} from "@angular/core";
import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs/Observable'
import "rxjs/Rx";
import {task} from './../models/taks.class'    // confirm it is "task" or "taks"
@Injectable()
export class TaskService {
  public API: string ='http://localhost:3000/tasks'

  constructor(public httpservice: HttpClient){}


getall(): Observable<task[]>{
  return this.httpservice
         .get(this.API)
         .map((response: Response) => {
             return response;
         })
         .catch(this.handleError);
}

 private handleError(error: Response) {
     return Observable.throw(error.statusText);
 }

}

您應該輸入GET請求。

return this.httpservice.get(this.API);

應該:

return this.httpservice.get<task[]>(this.API);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM