[英]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}`)
}
}
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.