Угловой 6: свойство 'of' не существует для типа 'typeof Observable'

Я использую Angular 6, используя "rxjs": "^ 6.0.0",

ОШИБКА: свойство "of" не существует для типа typeof Observable.

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',
    });
  }
}

person HD..    schedule 15.06.2018    source источник


Ответы (6)


Поскольку RxJS 6, правильный и рекомендуемый способ использования of() (RxJS 5 в Observable.of()) таков:

import { of } from 'rxjs';

Я думаю, что этот import { of } from 'rxjs/observable/of'; будет работать, только если у вас установлен rxjs-compat пакет.

person martin    schedule 15.06.2018
comment
Важно отметить, что вы должны изменить return Observable.of на просто return of - person Rod Astrada; 02.08.2018

В rxjs есть обновления: (его rxjs6)

import { of } from 'rxjs';

Он будет работать, только если в вашем приложении установлен пакет rxjs-compat.

Вы можете импортировать of из rxjs:

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',
    });
  }
}
person Shubham Verma    schedule 21.06.2018

Это работает для меня.

Угловой 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.');


      });
person englishPete    schedule 01.09.2018

С выпуском версии 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';
person HD..    schedule 15.06.2018

Вам необходимо импортировать of из rxjs/observable/of

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

Использование:

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

Обновление: для rxjs версии 6 без rxjs-compat вам необходимо импортировать of из самого rxjs, как указано @martin.

import { of } from 'rxjs';

Руководство по переходу на rxjs6

person Suraj Rao    schedule 15.06.2018

Решение состоит в том, чтобы вернуть (..) напрямую:

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