Angular 7 - getting error: 'rxjs has no exported member 'Observable'
10,455
Solution 1
To be compliant with rxjs6 and angular 7, you have to replace:
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
by:
import { map } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
And import Observable like that:
import { Observable } from 'rxjs';
not like that:
import { Observable } from 'rxjs/Observable';
// or import 'rxjs/add/observable';
You may need to delete node_modules
folder and launch npm install
, because it seems you have some wrong packages in it.
Solution 2
Your code is written in old angular version. changes required are
1. Use of Http is deprecated
2. importing map & Observable
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class EmpService {
employees=[];
constructor(private _http: HttpClient) { }
addEmployee(info){
return this._http.post("http://localhost/data/insert.php",info)
.pipe(
map(() => '' )
)
}
}
refer:
Author by
Mirshad
Updated on July 10, 2022Comments
-
Mirshad almost 2 years
This is my
package.json
file:"dependencies": { "@angular/animations": "~7.1.0", "@angular/common": "~7.1.0", "@angular/compiler": "~7.1.0", "@angular/core": "~7.1.0", "@angular/forms": "~7.1.0", "@angular/material": "^7.1.1", "@angular/platform-browser": "~7.1.0", "@angular/platform-browser-dynamic": "~7.1.0", "@angular/router": "~7.1.0", "core-js": "^2.5.4", "ng2-opd-popup": "^1.1.21", "rxjs": "~6.3.3", "tslib": "^1.9.0", "zone.js": "~0.8.26"
service.ts
file code as below:import { Injectable } from '@angular/core'; import { Http, Headers} from '@angular/http'; import 'rxjs/add/operator/map'; //import { Observable } from 'rxjs/Observable'; //import 'rxjs/add/observable'; import { Observable} from 'rxjs'; @Injectable({ providedIn: 'root' }) export class EmpService { employees=[]; constructor(private _http: Http) { } addEmployee(info){ return this._http.post("http://localhost/data/insert.php",info) .map(()=>""); } }
I got the following error:
rxjs has no exported member 'Observable'
Is there any kind of issues in versions?
-
Mirshad over 5 yearsGetting the same error . "(as no exported member 'Observable'. node_modules/rxjs/Observable.d.ts(1,15): error TS2307: Cannot find module 'rxjs-compat/Observable'.)"
-
veben over 5 yearsI think you have some wrong packages in
node_modules
folder. Just delete it and launchnpm install
-
veben over 5 yearsYou import Observable like that
import { Observable } from 'rxjs';
, right ? -
Presto about 5 yearsyou have to import Observable from 'rxjs' in [email protected] like this: import { Observable } from 'rxjs';
-
rijin about 5 yearsBoth works.. anyway we are importing a single class from rxjs
-
Presto about 5 yearsBoth works but your import in [email protected] is really from 'rxjs-compat/Observable'. The good practice is don't import observable from rxjs-compat but use rxjs. The only good reason to use rxjs-compat is if you have dependencies that still rely on an older version of rxjs.
-
Presto about 5 yearsI read than all of deprecated things will be remove on version 7
-
Chinmoy about 5 yearsDON'T
import { Observable } from 'rxjs/Observable';
USEimport { Observable } from 'rxjs';