首页 文章

Angular 6:属性'of'在'typeof Observable'类型上不存在

提问于
浏览
5

我正在使用 Angular 6 使用 "rxjs": "^6.0.0",

ERROR :'typeof Observable'类型的属性'of'不存在 .

import { Injectable } from '@angular/core';
import { TranslateLoader } from '@ngx-translate/core';
import { Observable, Subject, pipe, of } from 'rxjs';


@Injectable()
export class MiTranslateLoaderService implements TranslateLoader {

  getTranslation(lang: string): Observable<any> {
    return Observable.of({
      lbl_select: 'Select',
    });
  }
}

6 回答

  • 0

    从RxJS 6开始,导入 of 的正确和推荐方法是:

    import { of } from 'rxjs';
    

    我认为只有在安装了 rxjs-compat 软件包时, import { of } from 'rxjs/observable/of'; 才会起作用 .

  • 9

    There are some updates in rxjs: ( Its rxjs6)

    import { of } from 'rxjs';
    

    它仅在您的应用程序安装了 rxjs-compat 软件包时才有效

    您可以从 rxjs 导入 of

    import { Observable,of } from 'rxjs';

    并简单地返回 of()

    return of({
          lbl_select: 'Select',
        });
    

    所以你的代码将是:

    import { Injectable } from '@angular/core';
    import { TranslateLoader } from '@ngx-translate/core';
    import { Observable, of } from 'rxjs';
    
    
    @Injectable()
    export class MiTranslateLoaderService implements TranslateLoader {
    
      getTranslation(lang: string): Observable<any> {
        return of({
          lbl_select: 'Select',
        });
      }
    }
    
  • 0

    这对我有用 .

    Angular CLI 6.0.8

    RxJS 6.2.2

    import {of} from 'rxjs/index';
    
    
    this.dataService.currentData
    
        .pipe(takeUntil(this.destroy$))
        .pipe(switchMap((myData:MyDataType) =>
          of(this.anotherService.get(myData._id))))
        .pipe(map((response) => {
             if(response instanceof Error) {
                console.log('error:');
                console.dir(response);
             }
             return response;
        }))
        .subscribe((data:any) => {
           doStuff(data);
          },
          response => {
            console.log('response error');
            console.log(response)
          },
          () => {
            console.log('response complete.');
    
    
          });
    
  • 4

    随着版本6的发布, RxJS 改变了其内部包结构

    https://www.academind.com/learn/javascript/rxjs-6-what-changed/#import-statement-update-path

    import 'rxjs/add/observable/of';
    // or 
    import { of } from 'rxjs/observable/of';
    
  • 1

    你需要从 rxjs/observable/of 导入 of

    import { of } from "rxjs/observable/of";
    

    用法:

    return of({
      lbl_select: 'Select',
    });
    

    更新:对于没有rxjs-compat的 rxjs version 6 ,您需要从@martin提到的 rxjs 本身导入 of .

    import { of } from 'rxjs';
    

    Migration guide to rxjs6

  • 0

    解决方案是直接返回(..):

    getTranslation(lang: string): Observable<any> {
        return of({
          lbl_select: 'Select',
        });
    

相关问题