9

I am using Angular 6 using "rxjs": "^6.0.0",

ERROR : Property 'of' does not exist on type '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',
    });
  }
}
Kalamarico
  • 5,466
  • 22
  • 53
  • 70
HD..
  • 1,456
  • 3
  • 28
  • 48

6 Answers6

21

Since RxJS 6 the correct and recommended way of using of() (RxJS 5 in Observable.of()) is this:

import { of } from 'rxjs';

I think this import { of } from 'rxjs/observable/of'; will work only while you have rxjs-compat package installed.

martin
  • 93,354
  • 25
  • 191
  • 226
9

There are some updates in rxjs: ( Its rxjs6)

import { of } from 'rxjs';

It will work only when your app has rxjs-compat package installed

You can import of from rxjs:

import { Observable,of } from 'rxjs';

And simply return of()

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

So your code will be:

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',
    });
  }
}
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
3

This is working for me.

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.');


      });
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
englishPete
  • 809
  • 10
  • 15
1

You need to import of from rxjs/observable/of

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

Usage:

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

Update: for rxjs version 6 without rxjs-compat, you need to import of from rxjs itself as mentioned by @martin.

import { of } from 'rxjs';

Migration guide to rxjs6

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

With the release of version 6, RxJS changed its internal package structure

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';
HD..
  • 1,456
  • 3
  • 28
  • 48
1

The solution is to return of(..) directly :

getTranslation(lang: string): Observable<any> {
    return of({
      lbl_select: 'Select',
    });
glennsl
  • 28,186
  • 12
  • 57
  • 75
A.Rekik
  • 43
  • 6