为什么会出现错误TS2339:类型'Subject <any>'不存在属性'scan'?

时间:2019-01-12 00:49:45

标签: angular rxjs

编辑:实际上,我现在想知道。 Rxjs是否已从6.x中的链接转移到管道?参见:https://stackoverflow.com/a/49811404/8207637


这是我第一次使用rxjs流。我正在尝试创建一些非常简单的错误,但是无论是从tslint还是在构建时,都不断收到此错误。

我花了几个小时进行谷歌搜索并试图找出答案。看到几个人建议直接导入运算符。但这需要将它们包装在pipe()中才能使用。我希望能够将它们链接在一起。

当我发现可能需要添加rxjs-compat来解决问题时,我已经开始整理一个StackBlitz示例。因此,我在StackBlitz示例 e voila!中添加了rxjs-compat。错误消失了。

因此,我想出了如果将软件包添加到我的项目中,则可以期待类似的结果。不!

不确定如何解决该问题。我已经链接到stackblitz,它显示了导入工作和构建工作(服务文件不执行任何操作)。

Stackblitz https://stackblitz.com/edit/angular-rxjs-init

这是我的项目文件(不是来自stackblitz)

package.json

{
  "name": "ng7-zprod",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^7.1.4",
    "@angular/cdk": "^7.2.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/material": "^7.2.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "@ncstate/sat-popover": "^3.1.0",
    "core-js": "^2.5.4",
    "jquery": "^1.9.1",
    "popper.js": "^1.14.6",
    "rxjs": "~6.3.3",
    "rxjs-compat": "^6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.11.4",
    "@angular/cli": "~7.1.3",
    "@angular/compiler-cli": "~7.1.0",
    "@angular/language-service": "~7.1.0",
    "@fortawesome/fontawesome-free": "^5.6.3",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "bootstrap": "^4.2.1",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }
}

Service.ts

import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs';
import { ZTask } from '../models/ztask';



const initialZTasks: ZTask[] = [];

interface IZTaskOperation extends Function {
  // tslint:disable-next-line:callable-types
  (ztasks: ZTask[]): ZTask[];
}

@Injectable({
  providedIn: 'root'
})
export class ZTasksService {
  newTasks: Subject<ZTask> = new Subject<ZTask>();

  ztasksList: Observable<ZTask[]>;

  ztasksListUpdates: Subject<any> = new Subject<any>();

  ztaskcreate: Subject<ZTask> = new Subject<ZTask>();

  constructor() {
    this.ztasksList = this.ztasksListUpdates
    .scan((ztasks: ZTask[],                     // this line is where I get the error
           operation: IZTaskOperation) => {
             return operation(ztasks);
           },
           initialZTasks)
    .publishReplay(1)
    .refCount();
  }
}

1 个答案:

答案 0 :(得分:1)

正如@dmcgrandle在他的评论中所说,Rxjs为了改变其目录语句而放弃了链接。

这实际上清除了导入语句。

据说我应该在安装rxjs-compat时修复它,但不是。不知道为什么。

但是使用管道现在是正确的方法。像这样:

import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs';
import { ZTask } from '../models/ztask';
import { scan, publishReplay, refCount } from 'rxjs/operators';



const initialZTasks: ZTask[] = [];

interface IZTaskOperation extends Function {
  // tslint:disable-next-line:callable-types
  (ztasks: ZTask[]): ZTask[];
}

@Injectable({
  providedIn: 'root'
})
export class ZTasksService {
  newTasks: Subject<ZTask> = new Subject<ZTask>();

  ztasksList: Observable<ZTask[]>;

  ztasksListUpdates: Subject<any> = new Subject<any>();

  ztaskcreate: Subject<ZTask> = new Subject<ZTask>();

  constructor() {
    this.ztasksList = this.ztasksListUpdates.pipe(
    scan((ztasks: ZTask[],                     
           operation: IZTaskOperation) => {
             return operation(ztasks);
           },
           initialZTasks),
    publishReplay(1),
    refCount()
   )
  }
}