javascript - Angular2 Http请求返回Observable而不带map方法

标签 javascript angular angular2-http

我已使用 HTTP_PROVIDERS 设置我的应用程序

bootstrap(AppComponent, [
  HTTP_PROVIDERS,
  ROUTER_PROVIDERS,
  provide(LocationStrategy, { useClass: HashLocationStrategy }),
  PostService
]);

我的服务就像

@Injectable()
export class PostService {
  posts = [];

  constructor(http: Http) {
    this.http = http;
  }
}

但是当我打电话时

this.http.get('/posts')

它返回 Observable其中没有 map方法,它让我发疯了好几个小时。我使用 Babel 来转译我的 javascript 代码。

最佳答案

新类型转换的Server Communication开发指南(最后)讨论/提到/解释了这一点:

The RxJS library is quite large. Size matters when we build a production application and deploy it to mobile devices. We should include only those features that we actually need.

Accordingly, Angular exposes a stripped down version of Observable in the rxjs/Observable module, a version that lacks almost all operators including the ones we'd like to use here such as the map method...

It's up to us to add the operators we need. We could add each operator, one-by-one, until we had a custom Observable implementation tuned precisely to our requirements.

例如,如上面 @Langley 的评论所示:

import 'rxjs/add/operator/map';

或者,如果我们很懒,我们可以引入全套运算符:

import 'rxjs/Rx';

关于javascript - Angular2 Http请求返回Observable而不带map方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35025547/

相关文章:

Angular 2.0 和 Http 请求。类型转换物体。类型转换错误

listview - NativeScript Angular 2 Firebase 列表不起作用

html - 任何适用的指令均未提供 FormControl

javascript - API 是否应该包含用户信息?

unit-testing - 如何模拟服务?

php - 在 PHP 中删除 Angular2 应用程序中的数据

javascript - 将文本从 <span> 复制到剪贴板

javascript - 如何让元素适合屏幕?

javascript - 如何在Vuejs中隐藏复选框点击的内容?

javascript - ngCordova getUUID() 返回设备未定义