rxjs / Subscription没有导出成员'Subscription'

时间:2018-05-04 13:23:30

标签: angular npm rxjs rxjs-compat

我将我的角度项目和所有依赖项更新到最新版本。我没有遇到太多麻烦,我解决了大多数依赖问题,但我仍然坚持使用RxJs。这是我的package.json:

  "dependencies": {
    "@angular-devkit/build-angular": "^0.6.0",
    "@angular/animations": "^6.0.0",
    "@angular/common": "^6.0.0",
    "@angular/compiler": "^6.0.0",
    "@angular/core": "^6.0.0",
    "@angular/forms": "^6.0.0",
    "@angular/http": "^6.0.0",
    "@angular/platform-browser": "^6.0.0",
    "@angular/platform-browser-dynamic": "^6.0.0",
    "@angular/router": "^6.0.0",
    "angular-bootstrap-md": "^6.0.1",
    "core-js": "^2.5.5",
    "font-awesome": "^4.7.0",
    "rxjs": "^6.1.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/cli": "~6.0.0",
    "@angular/compiler-cli": "^6.0.0",
    "@angular/language-service": "6.0.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "electron": "^1.8.3",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.2",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.4.2",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^1.0.0",
    "protractor": "~5.3.1",
    "ts-node": "~6.0.2",
    "tslint": "~5.10.0",
    "typescript": "~2.7.2"

我只是从RxJs导入模块:

import { fromPromise } from 'rxjs/observable/fromPromise';
import { Subscription } from 'rxjs/Subscription';

他们都给出了同样的错误:

    [ts] Module '"***/node_modules/rxjs/Subscription"' has no exported
 member 'Subscription'.

fromPromise完全相同。以下是我从Subscribtion.d.ts收到的错误消息(我没有以任何方式修改过代码)

enter image description here

更新:

下面的答案解决了Subscription的问题,但fromPromise仍然会出现同样的问题,即使它已正确导出:

export * from 'rxjs-compat/observable/fromPromise';

enter image description here

4 个答案:

答案 0 :(得分:30)

RxJS 6有很多重大变化。例如,原型方法为

myObservable.map(data => data * 2)

将不再有效,必须由

替换
myObservable.pipe(map(data => data * 2))

所有详情均可在此处找到:https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md

在您修复所有重大更改之前,您可以使用rxjs-compathttps://github.com/ReactiveX/rxjs/tree/master/compat)重新使用旧代码。

  

此软件包需要向后兼容RxJS   它包含要添加运算符的导入   Observable.prototype和[{1}}的创建方法。

输入以安装它:

Observable

答案 1 :(得分:5)

我希望您的问题能够通过以下声明得到解决   import Subscription from 'rxjs'

答案 2 :(得分:2)

您可以通过以下方式解决此问题:

从“ rxjs”导入{来自};

并且,而不是: return Observable.fromPromise(new Promise((resolve,reject)=> {

现在就做:

从返回(新的承诺((解决,拒绝)=> {

Observable.of

也是如此

答案 3 :(得分:0)

afaik Angular 6和rxjs 6尚未兼容,因为他们创建的兼容性https://www.npmjs.com/package/rxjs-compat你必须安装

[UPDATE] fromPromise现在"来自"。 见这里:https://github.com/ReactiveX/rxjs/issues/3525