且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ts 属性 'map'不存在于类型 'Observable<Response>'

更新时间:2022-01-10 04:10:25

你必须在你的模块/组件/服务中引用 RxJs 库(无论你在这里写的是什么) 作为 Observable(从 http.get 返回的类型)是该库的一部分.RxJs 有很多你可以使用的操作符,比如 mapcatchdo 等等,但是为了使用您必须引用它们所在的文件/模块.

You have to reference the RxJs library in your module/component/service (whatever it is you are writing here) as Observable (the type returned from http.get) is a part of that library. The RxJs has many operators that you can use like map, catch, do, etc but in order to use these you must reference the files/modules that they are contained in.

angular 站点上的教程很好地解释了如何使用 Observable 以及如何创建引用映射到更常用的方法,例如 Observable代码>RxJs 库.通过创建一个包含对 RxJs 库中更常用的运算符和类型的引用的单个文件,您只需在要使用这些类型的地方引用该引用文件,就无需重新添加项目中每个文件中您想要利用它们的所有运算符/类型.

The tutorials on the angular site have a good explanation on how you consume the Observable<T> and how to create a reference mapping to the more common methods you want to use like map in the RxJs lib. By creating a single file with references to the more commonly used operators and types in the RxJs library you only have to then reference that reference file where you want to consume those types which saves on having to re-add all the operators/types in every file across your project where you want to take advantage of them.

这是一个示例文件(在本示例中名为 rxjs-operators.ts),其中包含一些更常用的方法.

Here is an example file (named rxjs-operators.ts for this example) with some of the more commonly used methods.

// Observable class extensions
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/throw';

// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';

在您要使用 .map(或任何其他方法)的文件顶部添加此行.

To the top of your file you want to use .map (or any other method) add this line.

import './rxjs-operators';