gpt4 book ai didi

angular - 错误 :Property 'map' does not exist on type 'Observable
转载 作者:行者123 更新时间:2023-12-02 16:27:35 24 4
gpt4 key购买 nike

在我的 Angular10 项目中,即使我 import 'rxjs/operator/map';import { map } from 'rxjs/operators';

>

服务代码是

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, filter, scan } from 'rxjs/operators';
import 'rxjs/add/operator/map';

@Injectable({
providedIn: 'root'
})
export class AuthService {

constructor( private http: HttpClient) { }

getAllPosts(){
return this.http.get('/posts').map((posts) => {
return posts;
});
}
}

我该如何克服这个错误?我的rxjs版本是6以上,angular版本是10。

最佳答案

你必须通过管道传递你的操作符。来自 the docs

Note that, for Angular apps, we prefer combining operators with pipes, rather than chaining. Chaining is used in many RxJS examples.

尝试:

return this.http.get('/posts').pipe(
map((posts) => {return posts; }),
);

关于angular - 错误 :Property 'map' does not exist on type 'Observable<Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64194762/

24 4 0