我有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)
我收到了这个
错误:无法指定类型&#39; Observable&#39;
我不知道如何解决这个问题
答案 0 :(得分:0)
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);
}
}
答案 1 :(得分:0)
您应该输入您的GET请求。
return this.httpservice.get(this.API);
应该是:
return this.httpservice.get<task[]>(this.API);