"rxjs" observable.throw is not a function - Angular4
The error There are multiple modules with names that only differ in casing.
is indicating the wrong import is being targeted with how you are trying to use Observable
.
The import should be with a capital "O" like:
import { Observable } from 'rxjs/Observable';
This will import the individual Observable
operator, which be used in combination with operators such as catch
or throw
on created Observables.
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
To import the full Observable object you'd import it like this:
import { Observable } from 'rxjs/Rx'
Update:
With newer versions of RxJS (5.5+) operators such as map()
and filter()
can used as pipeable operators in combination with pipe()
rather than chaining. They are imported such as:
import { filter, map, catchError } from 'rxjs/operators';
Keep in mind terms such as throw
are reserved/key words in JavaScript so the RxJS throw
operator is imported as:
import { _throw } from 'rxjs/observable/throw';
Update:
For newer versions of RxJS (6+), use this:
import { throwError } from 'rxjs';
and throw an error like this:
if (error.status === 404)
return throwError( new NotFoundError(error) )
In RxJS 6, Observable.throw()
is replaced by throwError()
which operates very similarly to its predecessor.
So, you can replace from Observable.throw(error)
to only throwError(error)
by importing:
import { throwError } from 'rxjs';
Checkout this link for further reference: https://www.metaltoad.com/blog/angular-6-upgrading-api-calls-rxjs-6