http - 缺少可观察方法 RxJS 5.0.0-beta.0

标签 http typescript browserify angular rxjs

我在将 RxJS 与 Angular 2 一起使用时遇到问题。Typescript 定义文件中建议的大多数方法都没有在我的 Observable 对象上定义,例如...

enter image description here

enter image description here

然后我发现,Observable 原型(prototype)上不存在该方法。

enter image description here

我知道从版本 4 到版本 5 有很多变化,所以我错过了什么吗?

Browserify 为我添加了它... enter image description here

最佳答案

没有看到您的实际代码,我无法确切地告诉您要添加什么来修复它。

但普遍的问题是:RxJS 5 不再包含在 Angular 2 中,因为它已经进入 Beta 阶段。您需要导入所需的运算符,或全部导入。导入语句如下所示:

import 'rxjs/add/operator/map'; // imports just map
import 'rxjs/add/operator/mergeMap'; // just mergeMap
import 'rxjs/add/operator/switchMap'; // just switchMap
import {delay} from 'rxjs/operator/delay'; // just delay

或者喜欢

import 'rxjs/Rx'; // import everything

要确定所需模块的路径,请查看 source tree .每次使用 add 导入都会将属性添加到 ObservableObservable.prototype。如果没有 add,您需要执行 import {foo} from 'rxjs/path/to/foo'

您还需要确保将 RxJS 正确引入项目。像这样的东西会进入你的 index.html 文件:

System.config({
    map: {
        'rxjs': 'node_modules/rxjs' // this tells the app where to find the above import statement code
    },
    packages: {
        'app': {defaultExtension: 'js'}, // if your app in the `app` folder
        'rxjs': {defaultExtension: 'js'}
    }
});
System.import('app/app'); // main file is `app/app.ts` 

如果您使用 Webpack to build the Angular 2 app like in this Github project (就像我做的那样),那么你不需要那个 System 的东西,导入应该做。

关于http - 缺少可观察方法 RxJS 5.0.0-beta.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34548924/

相关文章:

http - Websocket 情况 - 在端口 80 或 443 上,websocket 消息无法通过

android - 尝试登录网站...有点困惑

typescript :找不到名称要求

typescript 扩展枚举

gulp - 在 watchify 运行之前运行 eslint

javascript - browserify-shim 在 var 范围内时不导出隐式全局变量

node.js - Node 错误 : connect ETIMEDOUT

json - 带有 JSON 正文的 Mule ESB HTTP post

javascript - 如何以一种不会中断使用普通旧式 JavaScript 访问对象的方式导出 TypeScript 类

handlebars.js - 我应该如何构建我的模块以使用 hbsfy 和 browserify?