21

When I swithch to my Linux PC I get error like this:

app/app.module.ts(21,67): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(25,53): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(29,68): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(33,67): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(37,56): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(52,27): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(52,50): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(54,40): error TS2304: Cannot find name '_'.
app/app.module.ts(56,24): error TS2304: Cannot find name 'Observable'.
app/app.module.ts(58,24): error TS2304: Cannot find name 'Observable'.

Anyone know solution for this?

Also my editor show errors on HttpIntreceptor Class, when I remove it it works fine...

This part is marked: Observable<Response>

Could you check it:

class HttpInterceptor extends Http {

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router) {
        super(backend, defaultOptions);
    }

    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.request(url, options));
    }

    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.get(url,options));
    }

    post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {   
        return super.post(url, body);
    }

    put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.put(url, body, this.getRequestOptionArgs(options)));
    }

    delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.delete(url, options));
    }

    getRequestOptionArgs(options?: RequestOptionsArgs) : RequestOptionsArgs {
        if (options == null) {
            options = new RequestOptions();
        }
        if (options.headers == null) {
            options.headers = new Headers();
        }
        options.headers.append('Content-Type', 'application/json');
        return options;
    }

    intercept(observable: Observable<Response>): Observable<Response> {
        return observable.catch((err, source) => {
            if (err.status  == 401 && !_.endsWith(err.url, 'api/auth/login')) {

                return Observable.empty();
            } else {
                return Observable.throw(err);
            }
        });

    }
}
6
  • this is very broad, perhaps add the component you a receiving the error on. Commented Oct 11, 2016 at 17:30
  • It look like it is problem with Inerceptor could you check code, main post updated... I am new to typescript so I not sure where is problem... Commented Oct 11, 2016 at 17:36
  • yes you are right try digging through the http docs angular.io/docs/ts/latest/api/http/index/Http-class.html it looks like interceptor is outdated and not available anymore.I do not see it in the http class definition Commented Oct 11, 2016 at 17:41
  • 1
    Have you imported Rxjs - import 'rxjs/Rx'; ? Commented Oct 11, 2016 at 18:07
  • 1
    Yes that was problem I just figured it right now... import {Observable} from 'rxjs/Rx'; import 'rxjs/add/operator/map'; Commented Oct 11, 2016 at 18:09

4 Answers 4

43

I had the same issue fixed it by importing Observable

import {Observable} from 'rxjs/Rx';

Update wih RXJS v6

import { Observable } from 'rxjs';
Sign up to request clarification or add additional context in comments.

2 Comments

it is saying node_modules/rxjs/Rx"' has no exported member 'Observable'.
well it changed now. not it's import { Observable } from "rxjs/Observable";
21

You can do it in 2 ways

1. Import Observable and then import other functions like map, do, catch, throw whichever you are using

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
.....
.....
import 'rxjs/add/observable/throw';

2. Importing whole Rxjs

import {Observable} from 'rxjs/Rx';

or

import {Observable} from 'rxjs';

Its recommended to use first method, since importing whole rxjs is not necessary and will include all the sub-modules into the bundle affecting bundle size and load time

2 Comments

In angular 5, I used, option 1 to solve this issuse. It's working.
In angular 8 you can use import {Observable} from 'rxjs'; but not import {Observable} from 'rxjs/Observable'; or import {Observable} from 'rxjs/Rx';
7

None of the solutions worked, I had to use

import { Observable } from 'rxjs';

Comments

2

instead of using

import 'rxjs/add/operator/map';
this.obObservable().map(data => {})

use

import { map } from "rxjs/operators";
this.obObservable().pipe(map(data => {}))

angular changes it recently

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.