Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

Angular 6 RXJS Import Syntax?

I'm migrating an Angular 5 app to the latest CLI and Angular 6 RC and all of my Observable imports are broken. I see that Angular 6 changes the way the imports work, but I can't find any definite reference as to how the syntax works.

I had this in 5 and it worked fine:

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

Now with the new syntax I see that

import { Observable, Subject, throwError} from 'rxjs';
import { map } from 'rxjs/operators';

The first two lines compile, but I can't figure out how to get catch and throw for example. .map() also throws a build error when used in code.

Anybody have a reference to how this is supposed to work?

question from:https://stackoverflow.com/questions/49811177/angular-6-rxjs-import-syntax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From rxjs 5.5, catch has been renamed to catchError function to avoid name clash.

Due to having operators available independent of an Observable, operator names cannot conflict with JavaScript keyword restrictions. Therefore the names of the pipeable version of some operators have changed.

import { catchError } from 'rxjs/operators';

For throw you can use ErrorObservable.

import { ErrorObservable } from 'rxjs/observable/ErrorObservable';
ErrorObservable.create(new Error("oops"));

rxjs 6

Instead of ErrorObservable use throwError.

 import { throwError } from 'rxjs'
 throwError(new Error("oops"));

Also you will now have to pipe the operators instead of directly chaining them to the observable


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...