208

I was recently using observable on my authentication token like

Observable.of('token');

But it keeps giving me above mentioned error, though i have already imported this.

import {Observable} from 'rxjs/Observable';
Devesh Jadon
  • 7,004
  • 4
  • 22
  • 27

1 Answers1

487

You need to import it:

for Angular >= 6.0.0

uses RxJS 6.0.0 Angular Changelog 6.0.0

import { of } from 'rxjs';

And its usage has been changed, you no longer call it off of Observable:

of('token');

RxJS v5.x to v6 Update Guide - HowTo: Convert to pipe syntax which uses of()

for Angular <= 5.x.xx

import 'rxjs/add/observable/of';

And it's used as you have in your question

Observable.of('token');
Corey Cole
  • 2,262
  • 1
  • 26
  • 43
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 9
    i tried using this but it's still giving me the same error. – Devesh Jadon Jun 28 '16 at 05:26
  • but since we are using the properties of already imported object i.e Observable, do you think it's a good approach by angular2 to explicitly import different properties of already imported object? Please enlighten me about it. – Devesh Jadon Jun 28 '16 at 05:36
  • 3
    uh oh, i searched more about it. there were just some directory changes in recent rc may be. it works fine now. `import 'rxjs/add/observable/of';` – Devesh Jadon Jun 28 '16 at 05:38
  • 2
    @GünterZöchbauer I found I don't need to use this statement in one of angular projects. But in the other one, I have to import it. I don't understand the differences. Do you know the reasons? – niaomingjian Sep 14 '17 at 01:42
  • Can't tell without more information. Perhaps you don't use observable functions that need to be imported in this project. – Günter Zöchbauer Sep 14 '17 at 03:12
  • 1
    If you are having trouble, make sure you are importing from the OBSERVABLE directory and not the OPERATOR directory. – Alex Fallenstedt Dec 19 '17 at 19:47
  • This is not solving the issue. I am not seeing any other solutions. Are you aware of any other options? – chad Mar 16 '18 at 17:27
  • It did for many people. I don't know why it doesn't for you. You didn't privide any information. Perhaps it's better ti create a new question with your code and exact error and a reproduction in http://stackblitz.com – Günter Zöchbauer Mar 16 '18 at 18:44
  • 6
    They had some breaking changes in Angular 5/6 and RxJS. It is `import { of } from 'rxjs';` and `of('token');` now. See answers in the linked duplicate-of-question. – Martin Schneider Jun 29 '18 at 11:47
  • 31
    @MA-Maddin's answer worked for me. It's important to note not just the import statement, but also that the usage has changed. Instead of calling Observable.of(), you just call of(). – Derrick Miller Jul 03 '18 at 03:21
  • 2
    not working in angular 8 – TAHA SULTAN TEMURI Dec 02 '19 at 13:51
  • not working in angular 9 – Amit Bisht Feb 16 '20 at 18:43
  • Please check the Rxjs version installed, because in the official docs it's mentioned :import { of } from 'rxjs'; – Rebai Ahmed Jul 13 '21 at 23:33