无法访问Angular 2 http请求中的响应元素

时间:2017-06-09 05:55:12

标签: angular typescript angular-http

我有接口代码

export interface Books {
  artists: any;
  tracks: any;
}

我在服务中使用此界面来获取搜索结果。

getBook(keyword): Observable<Books>{
   let headers = new Headers();
   this.createAuthorizationHeader(headers);
    return this._http.get('https://api.spotify.com/v1/search?q=' + keyword + '&type=track,artist', {
    headers: headers
   })
  .map((response: Response) => <Books> response.json());
}

获得结果后,我想在组件中显示它

getBooks(value) {
  this._search.getBook(value)
  .subscribe(
  res => {
    this.books = res;
    this.artists = Object.assign({}, res.artists);
    console.log(this.artists);
  },
  error => { this.errMessage = <any>error }
  );
}

响应类似于

{
 href: "https://api.spotify.com/v1/search?query=muse&type=artist&market=US&offset=0&limit=20",
 items: [list of objects],
 limit: 20,
 total: 159,
}

console.log(this.artists)中我收到了上述对象,但是当我尝试访问 console.log(this.artists.items)中的项目时; < / strong>它给了我这个错误。

Property 'items' does not exist on type 'Object'

任何想法我都错过了???

0 个答案:

没有答案