简体   繁体   English

不可分配给“可观察”类型 <task> &#39;。角4

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

I have a problem with Observable type, any idea? 我对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. on the line return this.httpservice.get(this.API) 在线return this.httpservice.get(this.API)

I am getting this 我得到这个

error: is not assignable to type 'Observable' 错误:无法分配给“可观察”类型

I don`t know how to fix this problem 我不知道如何解决这个问题

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

}

You should type your GET request. 您应该输入GET请求。

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

should be: 应该:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型&#39;日期&#39;不能分配给&#39;Observable&#39;类型 <Date> &#39; - Angular 6+ - Type 'Date' is not assignable to type 'Observable<Date>' - Angular 6+ angular5:类型“true”不可分配给类型“Observable”<boolean> - angular5: Type 'true' is not assignable to type 'Observable<boolean> Angular 订阅不可分配给可观察的 - Angular subscription is not assignable to observable 角度:错误TS2322:类型&#39;Observable &lt;{}&gt;&#39;不可分配...使用share()运算符 - Angular: error TS2322: Type 'Observable<{}>' is not assignable … with share() operator 在angular2的服务层上修改对象:无法将订阅分配给可观察的类型 - Modifying object at service layer in angular2: Subscription is not assignable to the type observable Angular 7 Type&#39;Observable &lt;{main:HttpResponse <any> ; 尺寸:任何; &#39;&gt;&#39;不能赋值为&#39;Observable&#39; <Order[]> “ - Angular 7 Type 'Observable<{ main: HttpResponse<any>; size: any; }>' is not assignable to type 'Observable<Order[]>' 输入&#39;可观察的<Object> &#39; 不可分配给类型 &#39;Observable<IUser[]> &#39; - Type 'Observable<Object>' is not assignable to type 'Observable<IUser[]>' 可观察的 <void | AuthError> &#39;不可分配给&#39;Observable类型 <Action> - Observable<void | AuthError>' is not assignable to type 'Observable<Action> RXJS可观察的枚举类型,类型为“可观察” <Type> &#39;不可分配给&#39;Type&#39;类型 - RXJS Observable enum type, Type 'Observable<Type>' is not assignable to type 'Type' 无法将“订阅”类型分配给“可观察”类型 <Exercise[]> “ - Type 'Subscription' is not assignable to type 'Observable<Exercise[]>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM