Vladimir
Vladimir

Reputation: 1789

error TS2304: Cannot find name 'Observable' Angularjs 2

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

    }
}

Upvotes: 21

Views: 48549

Answers (4)

Taha Zgued
Taha Zgued

Reputation: 1108

I had the same issue fixed it by importing Observable

import {Observable} from 'rxjs/Rx';

Update wih RXJS v6

import { Observable } from 'rxjs';

Upvotes: 43

GiorgosK
GiorgosK

Reputation: 8299

None of the solutions worked, I had to use

import { Observable } from 'rxjs';

Upvotes: 7

rubal islam
rubal islam

Reputation: 359

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

Upvotes: 2

Kowsalya
Kowsalya

Reputation: 1406

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

Upvotes: 21

Related Questions