【发布时间】:2019-06-19 22:04:51
【问题描述】:
我正在尝试在 Angular 7 中使用 HttpClient 获取 JSON。代码工作正常,但我正在尝试实现注释代码并停止使用 CONST IMAGES 并直接从 API 获取此数据,但我不能做吧。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class ImageService {
// public _url: string = "https://jsonplaceholder.typicode.com/photos";
constructor(
// public http: HttpClient
) {}
theImages = [];
// IMAGES = [];
getImages(){
// this.IMAGES = this.http.get(this._url);
return this.theImages = IMAGES.slice(0);
}
getImage(id: number){
// this.IMAGES = this.http.get(this._url);
return IMAGES.slice(0).find(image => image.id == id);
}
}
const IMAGES = [
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "https://via.placeholder.com/600/92c952",
"thumbnailUrl": "https://via.placeholder.com/150/92c952"
},
...
错误代码是:
error TS2740: Type 'Observable<Object>' is missing the following properties from type 'any[]': length, pop, push, concat, and 25 more.
【问题讨论】:
-
http 方法会返回 observable,最好先阅读文档guide to http
标签: angular typescript angular7 angular2-observables angular-httpclient