angular - Observable 类型上不存在属性映射
问题描述
我试图从 Angular v4 更新到 Angular v6。我正在替换Http
andHttpModule
到HttpClient
and HttpClientModule
。所以我HttpClient
从@angular/common/http
服务中导入并试图从 DBpedia 的 API 中获取结果。我之前使用的是Http
from@angular/http
并且我的以下代码工作正常
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
函数在类型上不存在Observable<ArrayBuffer>
。进一步删除map
函数会为函数提供相同的消息catch
。我点击了这个链接https://github.com/angular/angular/issues/15548但其中提供的解决方案对我不起作用。我错了?我应该删除两者map
并catch
在getsearchresults(searchquery)
.
解决方案
基本上你面临的问题types
。上面的打字稿代码不一致。例如,没有为getsearchresults()
函数定义类型。没有定义类型this.http.get<NoTypeDefinedHere>
默认情况下进一步httpClient
给出 json 响应let headers = new Headers({ 'Accept': 'application/json' });
。这里可能不需要。
检查您是否在导入中使用http
模块或模块。httpClient
检查是否rxjs/add/operator/map
正确导入
推荐阅读
- r - 在 R 中,使用带有 rank() 的第二列来打破平局
- php - 为 json_decode 获取字符串清理
- python - python语言中的空类型/类的名称是什么
- java - 如何删除最后一行/组件/图标/小部件/等。来自 JTextPane
- java - 当几列中有行分隔值而不是逗号分隔时如何拆分csv文件
- java - 仅存储一个与 Hibernate 和 Spring 的双向 OneToOne 关系的实体
- integer - 有人利用 CVE-2017-7529 整数溢出吗?
- shopify - Shopify的主题编辑器中为什么不显示范围单位
- php - 如何使用 laravel 的集合访问对象?
- python - 我如何用一堆 if 语句来读取这个函数?