I'm not sure if this is an Angular CLI issue or not. But I thought I would bring it to your attention.
Services generated using the Webpack version of the Angular CLI version do not have the Observable.throw
method defined at runtime.
import { Http, Response } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
@Injectable()
export class ApiService {
constructor(private http: Http) { }
fetchRemoteData(): Observable<any> {
const invalidUrl = 'https://api.somedomain.com/an-invalid-path';
return this.http.get(invalidUrl)
.map((response: Response) => {
return <any>response.json();
})
.catch(this.handleServerError);
}
private handleServerError(error: Response) {
return Observable.throw(error.json().error || 'Server error'); // Observable.throw() is undefined at runtime using Webpack
}
}
Observable.throw
is undefined
.TypeError: __WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.throw is not a function
Refer to this _StackOverflow_ post.
Oops, a simple mistake. I solved this by adding import 'rxjs/add/observable/throw';
. I had this line commented out (though not included in this issue's submission) because I was using /operator/
rather than /observable/
which doesn't exist.
I solved this by using: import { Observable } from 'rxjs/Rx';
Instead of: import { Observable } from 'rxjs/Observable';
Answer also available at: https://github.com/ReactiveX/rxjs/issues/1866
thanks @CalvinDale !
@nader30 Might work, but not recommendable. See this comment in same thread you posted.
@Groschenroman do you have better idea for this error and does not import EVERYTHING?
@Aralhi just import what you need like it said in their linked comment:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
thanks @adamdport :)
I'm running Angular 7.0.4 and the accepted "right way" to fix this is to have the following imports:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
However, I already had the correct imports. And the only way I've found so far to get past this issue is to switch the Observable to import from 'rxjs/Rx'.
It sounds like this is not recommended, but as of yet, I have not seen a better solution.
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
_This action has been performed automatically by a bot._
Most helpful comment
Oops, a simple mistake. I solved this by adding
import 'rxjs/add/observable/throw';
. I had this line commented out (though not included in this issue's submission) because I was using/operator/
rather than/observable/
which doesn't exist.