Property 'of' does not exist on type 'typeof Observable [duplicate]
Asked Answered
W

1

211

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';
Whitcher answered 28/6, 2016 at 5:18 Comment(1)
import { of } from 'rxjs'; and use it like this of('token'); without ObservableMiserable
S
494

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');
Shipp answered 28/6, 2016 at 5:22 Comment(13)
i tried using this but it's still giving me the same error.Whitcher
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.Whitcher
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';Whitcher
@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?Mora
Can't tell without more information. Perhaps you don't use observable functions that need to be imported in this project.Platitudinous
If you are having trouble, make sure you are importing from the OBSERVABLE directory and not the OPERATOR directory.Eigenfunction
This is not solving the issue. I am not seeing any other solutions. Are you aware of any other options?Constanta
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 stackblitz.comPlatitudinous
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.Zombie
@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().Reynoso
not working in angular 8Jalisajalisco
not working in angular 9Rorqual
Please check the Rxjs version installed, because in the official docs it's mentioned :import { of } from 'rxjs';Soult

© 2022 - 2024 — McMap. All rights reserved.