typescript - Angular2 http 缺少 .map 函数

标签 typescript angular

<分区>

我在 tutorial 中找到了这个示例代码:

getRandomQuote() {
  this.http.get('http://localhost:3001/api/random-quote')
    .map(res => res.text())
    .subscribe(
      data => this.randomQuote = data,
      err => this.logError(err),
      () => console.log('Random Quote Complete')
    );
}

但是当尝试使用它时,我只得到 TypeError: this.http.get(...).map is not a function in [null]:

getChannels():Promise<Channel> {
    return this.http.get('...')
        .map(function (response:Response) {
            ...
        }).toPromise();
}

我的 Typescript 编译器告诉我这些方法是可用的,但是在检查 http.get() 的返回值时,它们丢失了。

我用的是当前angualar2入门指南的package.json:

"dependencies": {
  "angular2": "2.0.0-beta.0",
  "systemjs": "0.19.6",
  "es6-promise": "^3.0.2",
  "es6-shim": "^0.33.3",
  "reflect-metadata": "0.1.2",
  "rxjs": "5.0.0-beta.0",
  "zone.js": "0.5.10"
},

...

<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>

有什么想法我现在可能会出错吗?

最佳答案

Observable 默认情况下只有几个运算符。您必须显式导入它们:

import 'rxjs/add/operator/map';

或者,如果您不想考虑它,只需加载所有内容(例如,在您的 Bootstrap 文件中):

import 'rxjs/Rx';

关于typescript - Angular2 http 缺少 .map 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581471/

相关文章:

angular - 尽管通过了审核,但 pwa 无法通过 chrome 安装

javascript - 如何向数组添加新元素而不覆盖第一个元素?

typescript - 我不是这个意思 我是这个意思

angular - routerLinkActive 允许任何查询参数

angular - 通过重定向登录后,Firebase Datasnapshot Observable 未立即更新

jquery - 使用 jQuery 和 jQuery UI 运行任何 Jest 测试时出现问题

angular - 如何保持 RxJ 干燥

typescript - 在 Typescript 中使用字符串文字类型

typescript - Typescript 中的 Observable<void> 使用模式

typescript - Typescript 中函数声明的可重用类型注释?