Angular - 从 API 获取数据

sos*_*ick 2 typescript angular

我必须从 flickr API 获取数据。正是我需要来自我的 URL 的“照片”数组。当我获取 app.component 中的数据并对其进行操作时,它会起作用,但我知道这不是一个好的解决方案。

我试过:

照片.ts:

  export class Photo {
    title: string;
    farm: number;
    secret: string;
    server: string;
    owner: string;
    id: string;
  };
Run Code Online (Sandbox Code Playgroud)

app.component.ts:

  export class AppComponent implements OnInit {

    photos: Photo[];

    constructor(private http: HttpService) {}

    ngOnInit() {
      this.getData();
    }

    getData() {
      this.http.getData().subscribe(data => {
        this.photos = data;
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)

和我的主要问题,http.service.ts:

export class HttpService {

  constructor(private http: HttpClient) { }

  getData(): Observable<Photo[]> {
    return this.http.get('https://api.flickr.com/path')
      .map(res => res.photos.photo);
  }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,一切都应该正常工作,但我收到一个错误:

ERROR in src/app/http.service.ts(18,23): error TS2339: Property 'photos' does not exist on type 'Object'.
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 6

尝试将 res 隐式设置为“any”

getData(): Observable<Photo[]> {
  return this.http.get('https://api.flickr.com/path')
    .map((res:any) => res.photos.photo);
}
Run Code Online (Sandbox Code Playgroud)