我知道之前已经被问过多次了,但是,我无法弄清楚如何使用以下帖子更改自己的代码。
Property 'subscribe' does not exist on type 'OperatorFunction<Response, Recipe[]>'
Property 'subscribe' does not exist on type 'OperatorFunction<Product[], ParamMap>'
Property 'subscribe' does not exist on type 'OperatorFunction<unknown, any>'
rest-api.service.ts
specificObjectRequest(ids: string[]): Observable<any> {
var idString = ids.join();
const url = `${ApiServerUrl}/media_objects?ID=${idString}`;
console.log("requesting from url: " + url);
this.http.get(url, {}, {})
.then(res => this.extractData = res.data)
.catch(this.handleError);
return;
}
tagRequest(topic: string){
var url = `${ApiServerUrl}/tags?topic=${topic}`;
url = encodeURI(url);
console.log("requesting from url: " + url);
this.http.get(url, {}, {})
.then(res => this.extractData = res.data)
.catch(this.handleError);
return map(this.extractData);
}
我使用官方文档https://ionicframework.com/docs/native/http和本教程
创建了此请求filter.page.ts
async requestMapTags(topic: string){
await this.api.tagRequest(topic).subscribe(res => { //Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>'
console.log(res);
this.tagsRequestAnswer = res;
}, err => {
console.log(err);
});
}
错误消息:
[ng] ERROR in src/app/filter/filter.page.ts(89,35): error TS2339: Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>'.
据我了解,这是Promises与rxjs之间的冲突。
问题: 我需要进行哪些更改才能使其正常工作?有没有一种方法可以在我的代码中添加所需的“管道”函数以使其正常工作?
答案 0 :(得分:1)
您的tagRequest()
的返回值必须为Observable
而不是OperatorFunction<Response, {}>
所以尝试这样的事情:
import { from } from 'rxjs';
tagRequest(topic: string){
var url = `${ApiServerUrl}/tags?topic=${topic}`;
url = encodeURI(url);
console.log("requesting from url: " + url);
return from(this.http.get(url, {}, {}));
}
答案 1 :(得分:1)
在这里使用Promise似乎是不必要的,您可以轻松完成
import { of } from 'rxjs';
tagRequest(topic: string){
var url = `${ApiServerUrl}/tags?topic=${topic}`;
url = encodeURI(url);
return of(this.http.get(url, {}, {})).pipe(map(res => res.data));
}
requestMapTags(topic: string){
this.api.tagRequest(topic).subscribe(res => {
console.log(res);
this.tagsRequestAnswer = res;
}, err => {
console.log(err);
});
}