类型Observable <arraybuffer>

时间:2018-05-13 10:57:30

标签: angular typescript angular4-httpclient angular6

我试图从Angular v4更新到Angular v6。 我正在将HttpHttpModule替换为HttpClientHttpClientModule。 因此,我在HttpClient服务中导入@angular/common/http,并尝试从DBpedia的API中获取结果。早些时候我使用Http中的@angular/http并且我的以下代码工作正常< / p>

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Jsonp, Headers, RequestOptions, URLSearchParams} from "@angular/http";
import {Store} from "@ngrx/store";
import * as fromRoot from '../reducers';
import {Observable} from "rxjs";

@Injectable()
export class KnowledgeapiService {

  server = 'http://lookup.dbpedia.org';
  searchURL = this.server + '/api/search/KeywordSearch?';
  homepage = 'https://susper.com';
  logo = '../images/susper.svg';

  constructor(
    private http: HttpClient,
    private jsonp: Jsonp,
    private store: Store<fromRoot.State>
  ) {}

  public getsearchresults(searchquery){
    let params = new URLSearchParams();

    params.set('QueryString', searchquery);

    let headers = new Headers({ 'Accept': 'application/json' });
    let options:any = new RequestOptions({ headers: headers, search: params });

    return this.http
      .get(this.searchURL, options).map(res =>

        res.json()

      ).catch(this.handleError);
  }

  private handleError (error: any) {
    // In some advance version we can include a remote logging of errors
    let errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // Right now we are logging to console itself
    return Observable.throw(errMsg);
  }
}

但是现在当我从Http依赖项迁移到HttpClient时,在函数getsearchresults(searchquery)中每当我将map函数映射数据用于json时,它都会出现{{{类型map上不存在1}}函数。进一步删除Observable<ArrayBuffer>函数会为map函数提供相同的消息。我按照这个链接https://github.com/angular/angular/issues/15548但是它提供的解决方案对我不起作用。我错在哪里?我应该删除catch中的mapcatch功能。

1 个答案:

答案 0 :(得分:1)

基本上,您遇到了types的问题。上面的打字稿代码不一致。例如,没有为getsearchresults()函数定义类型。 this.http.get<NoTypeDefinedHere>

中未定义任何类型

默认情况下,httpClient会给出json响应。此处可能不需要let headers = new Headers({ 'Accept': 'application/json' });

检查您是否在导入中使用http模块或httpClient模块。

检查您是否正确导入了rxjs/add/operator/map