错误 TS2339:类型'Observable<Foo>'上不存在属性'takeUntil',以及其他rxjs v.6错误。

15

我最近在我的Angular项目中更新了很多包。

旧的package.json:

{
  "name": "data-jitsu",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.10",
    "@angular/cdk": "^5.2.5",
    "@angular/common": "5.2.7",
    "@angular/compiler": "5.2.7",
    "@angular/core": "5.2.7",
    "@angular/forms": "5.2.7",
    "@angular/http": "5.2.7",
    "@angular/material": "^5.2.5",
    "@angular/platform-browser": "5.2.7",
    "@angular/platform-browser-dynamic": "5.2.7",
    "@angular/router": "5.2.7",
    "@types/youtube": "0.0.29",
    "angular-froala-wysiwyg": "^2.7.2-1",
    "angular2-materialize": "^15.1.10",
    "angularfire2": "^4.0.0-rc0",
    "core-js": "^2.4.1",
    "d3": "^4.13.0",
    "firebase": "^3.9.0",
    "hammerjs": "^2.0.8",
    "jquery": "^3.0.0",
    "materialize-css": "^0.100.2",
    "ngx-youtube-player": "0.0.41",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.7.2",
    "@angular/compiler-cli": "5.2.7",
    "@types/bootstrap": "^3.3.36",
    "@types/d3": "^4.13.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "2.6.2"
  }
}

新的 package.json:

{
  "name": "data-jitsu",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/cdk": "^6.3.1",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/material": "^6.2.0",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "@types/youtube": "^0.0.29",
    "angular-froala-wysiwyg": "^2.7.2-1",
    "angular2-materialize": "^15.1.10",
    "angularfire2": "^5.0.0-rc.11",
    "core-js": "^2.4.1",
    "d3": "^4.13.0",
    "firebase": "^5.0.3",
    "hammerjs": "^2.0.8",
    "jquery": "^3.0.0",
    "materialize-css": "^0.100.2",
    "ngx-youtube-player": "0.0.41",
    "rxjs": "^6.2.1",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/bootstrap": "^3.3.36",
    "@types/d3": "^4.13.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^10.5.1",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

当我使用新的package.json更新运行npm install,然后运行ng serve时,我会得到许多编译错误:
src/app/all-matches/all-matches.component.ts(35,39):错误 TS2339:类型“Observable”上不存在属性“takeUntil”。 src/app/app.component.ts(28,39):错误TS2339:类型“Observable”上不存在属性“takeUntil”。 src/app/authorization.service.ts(19,41):错误TS2339:类型“typeof Observable”上不存在属性“of”。 src/app/authorization.service.ts(29,37):错误TS2339:类型“typeof Observable”上不存在属性“of”。 src/app/authorization.service.ts(42,39):错误TS2339:类型“typeof Observable”上不存在属性“of”。 src/app/authorization.service.ts(66,39):错误TS2339:类型“typeof Observable”上不存在属性“of”。 src/app/authorization.service.ts(74,37):错误TS2339:类型“typeof Observable”上不存在属性“of”。 src/app/authorization.service.ts(82,39):错误TS2339:类型“typeof Observable”上不存在属性“of”。 src/app/database.service.ts(7,31):错误TS2305:“/Users/mf/Desktop/dataJitsu/node_modules/angularfire2/database/index”模块没有导出“FirebaseListObservable”成员。 src/app/database.service.ts(7,55):错误TS2305:“/Users/mf/Desktop/dataJitsu/node_modules/angularfire2/database/index”模块没有导出“FirebaseObjectObservable”成员。 src/app/match-display/match-display.component.ts(27,49):错误TS2339:类型“AngularFireObject<{}>”上不存在属性“takeUntil”。 src/app/new-match/new-match.component.ts(10,30):错误TS2305:“/Users/mf/Desktop/dataJitsu/node_modules/angularfire2/database/index”模块没有导出“FirebaseListObservable”成员。 src/app/new-match/new-match.component.ts(10,54):错误TS2305:“/Users/mf/Desktop/dataJitsu/node_modules/angularfire2/database/index”模块没有导出“FirebaseObjectObservable”成员。 src/app/new-match/new-match.component.ts(130,37):错误TS2339:类型“Observable”上不存在属性“switchMap”。 src/app/protection.guard.ts(14,34):错误TS2339:类型“Observable”上不存在属性“map”。 src/app/test-db/test-db.component.ts(7,30):错误TS2305:“/Users/mf/Desktop/dataJitsu/node_modules/angularfire2/database/index”模块没有导出“FirebaseListObservable”成员。 src/app/test-db/test-db.component.ts(7,54):错误TS2305:“/Users/mf/Desktop/dataJitsu/node_modules/angularfire2/database/index”模块没有导出“FirebaseObjectObservable”成员。 src/app/user-status-report/user-status-report.component.ts(28,39):错误TS2339:类型“Observable”上不存在属性“takeUntil”。 src/app/user-status-report/user-status-report.component.ts(33,45):错误TS2339:类型“AngularFireObject<{}>”上不存在属性“subscribe”。 src/app/user-status-report/user-status-report.component.ts(35,56):错误TS2339:类型“AngularFireObject<{}>”上不存在属性“takeUntil”。 src/app/user-status-report/user-status-report.component.ts(43,66):错误TS2339:类型“AngularFireObject<{}>”上不存在属性“takeUntil”。
大多数错误似乎与rxjs有关,我认为是正确的,因为我在更新中使用了rxjs v.6,并且我的理解是,在v.5和v.6之间许多事情都被破坏了。
我尝试安装rxjs-compatnpm install rxjs@6 rxjs-compat@6 --save,但我可以确认这个包除了增加一些负担外,没有解决我看到的任何错误。
我还根据这里这里的建议运行了ts-lint来自动更新。
npm i -g rxjs-tslint
rxjs-5-to-6-migrate -p [path/to/tsconfig.json]

我收到了以下信息:

JavaScript文件没有指定有效的规则

我猜这意味着在使用rxjs时我没有使用正确的ts约定?

当涉及到rxjs时,我还是很无能的,所以我不太清楚需要重构什么。

但我认为我的问题实际上出现在重构之前:

我查看了第一个错误(

错误TS2339:类型“Observable”上不存在属性“takeUntil”

)。 这个错误来自于我的身份验证服务中的一个方法。 在此服务的导入语句中(以及大多数其他.ts文件),我的IDE突出显示了许多“找不到模块foo”的错误(例如,“找不到模块'rxjs'”对于导入语句import { Observable } from 'rxjs';)。

同一文件中的类似错误:

import { Injectable, EventEmitter } from '@angular/core';(“找不到模块”@angular / core“)

import * as firebase from 'firebase/app';(“找不到模块”firebase / app“)

import { Router } from '@angular/router';(“找不到模块”@angular / router“)

这是我的app.module.ts文件,如果有用的话:

import { MaterializeModule } from 'angular2-materialize'
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { masterFirebaseConfig } from './api-keys';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { AppComponent } from './app.component';
import { YoutubeComponent } from './youtube/youtube.component';
import { routing } from './app.routing';
import { NewMatchComponent } from './new-match/new-match.component';
import { CreateAccountComponent } from './create-account/create-account.component';
import { TestDbComponent } from './test-db/test-db.component';
import { LandingComponent } from './landing/landing.component';
import { MatchDisplayComponent } from './match-display/match-display.component';
import { AuthorizationService } from './authorization.service';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { DatabaseService } from './database.service';
import { TextTransformationService } from './text-transformation.service';
import { ValidationService } from './validation.service';
import { LoginComponent } from './login/login.component';
import { ProtectionGuard } from './protection.guard';
import { AllMatchesComponent } from './all-matches/all-matches.component';
import { AnnotationDisplayComponent } from './annotation-display/annotation-display.component';
import { D3Service } from './d3.service';
import { NotfoundComponent } from './notfound/notfound.component';
import { UserStatusReportComponent } from './user-status-report/user-status-report.component';
import { PaymentOrAnnotationDetailsComponent } from './payment-or-annotation-details/payment-or-annotation-details.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { MatSelectModule } from '@angular/material/select';
import { MatOptionModule } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material';
import { MatTableModule } from '@angular/material/table';
// import { MatTreeModule } from '@angular/material';
import { MatTreeModule } from '@angular/material/tree';
import { MatSortModule } from '@angular/material';
import { MatDatepickerModule, MatNativeDateModule, MatPaginatorModule } from '@angular/material';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import { CdkTreeModule } from '@angular/cdk/tree';


export const firebaseConfig = {
  apiKey: masterFirebaseConfig.apiKey,
  authDomain: masterFirebaseConfig.authDomain,
  databaseURL: masterFirebaseConfig.databaseURL,
  storageBucket: masterFirebaseConfig.storageBucket
};

@NgModule({
  declarations: [
    AppComponent,
    YoutubeComponent,
    NewMatchComponent,
    CreateAccountComponent,
    TestDbComponent,
    LandingComponent,
    MatchDisplayComponent,
    LoginComponent,
    AllMatchesComponent,
    AnnotationDisplayComponent,
    NotfoundComponent,
    UserStatusReportComponent,
    PaymentOrAnnotationDetailsComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routing,
    MaterializeModule,
    ReactiveFormsModule,
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFireDatabaseModule,
    AngularFireAuthModule,
    BrowserAnimationsModule,
    MatSlideToggleModule,
    CdkTreeModule,
    MatSelectModule,
    MatOptionModule,
    MatInputModule,
    MatNativeDateModule,
    MatDatepickerModule,
    MatTableModule,
    MatSelectModule,
    MatSortModule,
    MatProgressSpinnerModule,
    MatPaginatorModule,
    MatTreeModule
  ],
  providers: [AuthorizationService, DatabaseService, ProtectionGuard, D3Service, ValidationService, TextTransformationService],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是包含所有这些问题的存储库分支。

git clone https://github.com/Atticus29/dataJitsu.git
cd dataJitsu
git checkout version-hell-SO 
npm install
ng serve

欢迎并鼓励提供任何解决导入问题的故障排除提示或直接解决方案!


只需添加 import 'rxjs/add/operator/takeUntil'; - Vasyl
1个回答

48

看起来你的操作符仍然是以rxjs 5.x的方式链接的。

因此,回顾一下rxjs6中发生了什么:

  1. 导入不同了。现在,你应该从'rxjs'导入Observable、Subject、BehaviorSubject等和之前在'rxjs/add/observable'中方法不同的方法。例如:

    import {Observable, Subject, of, from} from 'rxjs';

    或者,所有诸如map、concat、do(现在称为tap)等运算符都要从rxjs/operators导入。所以像这样:

    import { map, tap, takeUntil} from 'rxjs/operators';

    我认为你的导入方式非常合理。

  2. 你必须使用管道代替链接运算符。

    例如,在你的all-matches.components.ts的第33行,如果你替换: this.authService.getCurrentUser().takeUntil(this.ngUnsubscribe).subscribe(user=>{

    通过

    this.authService.getCurrentUser().pipe(takeUntil(this.ngUnsubscribe)).subscribe(user=>{

    你的takeUntil错误将消失。

    同样地,不要用Observable.of(true),你应该导入of运算符并使用of(true)。你可以在authorization.service.ts文件中尝试这个方法,所有的“of错误”都会被修复。

    也许你应该检查一下这个

  • 关于自定义文件导入错误,请检查文件是否存在。例如:

    ERROR in src/app/app.module.ts(6,38): error TS2307: 无法找到模块 './api-keys'

    import { masterFirebaseConfig } from './api-keys'

    相关是正常的,因为文件'./api-keys'不存在。 PS:如果密钥未存储在git中,则可能不会出现这些错误。

  • 最后,关于AngularFire5.0,您应该使用以下运算符之一:

    Db.list('items').subscribe(console.log)

    变为(使用valueChanges方法):

    Db.list<Item>('items').valueChanges().subscribe(console.log)

    更多信息: https://github.com/angular/angularfire2/blob/master/docs/version-5-upgrade.md

  • 祝好运!


    1
    谢谢!问题已解决。很抱歉没有提供 API 密钥文件。我故意将其添加到了 .gitignore 文件中,以免被公开访问。 - Atticus29

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接