【发布时间】:2019-12-28 13:41:03
【问题描述】:
代码返回错误 Type 'Subscription' is missing the following properties from type 'ToDo[]': length, pop, push, concat, and 26 more.
这通常是由于返回的 observable 没有被设置为数组。在我的情况下,返回类型是一个数组,但这个错误不断破坏我的应用程序。尽管以前工作过。
toDoList: ToDo[];
this.usertodo = this.getUserTodo(this._userId);
getUserTodo(id: number): ToDo[] {
return this.DataService.apiGetUserTodo(id).subscribe(
todo => {
this.toDoList = todo;
}
);
}
//在数据服务中
apiGetUserTodo(id: number): Observable<ToDo[]> {
const url = `${this.APIurl}/users/${id}/todos`;
return this.http.get<ToDo[]>(url).pipe(
tap(data => console.log('USER ' + JSON.stringify(data)))
);
}
这应该没有错误,但是会出现此消息
src/app/todo/todo.component.ts:28:8 中的错误 - 错误 TS2740:“订阅”类型缺少“ToDo[]”类型的以下属性:长度、弹出、推送、连接和还有 26 个。
【问题讨论】:
标签: angular http https subscribe