Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 737
Rick Strahl Avatar asked Apr 13 '18 07:04

Rick Strahl


People also ask

How do I import RxJS operators in Angular 6?

Run these 2 commands after running ng update . This should fix the rxjs imports: npm i -g rxjs-tslint. rxjs-5-to-6-migrate -p src/tsconfig.

What is the use of RxJS in Angular 6?

RxJS is a popular library among web developers. It provides functional and reactive programming patterns for working with events and streams of data and has been integrated into many web development libraries and frameworks such as Angular.

How do I add RxJS in Angular 8?

To install or update an Angular CLI type this command. Next, create a new Angular 8 app using Angular CLI by type this command. That command will create a new Angular 8 app with the name `angular-observable-rxjs` and pass all questions as default then the Angular CLI will automatically install the required NPM modules.


3 Answers

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

like image 127
Suraj Rao Avatar answered Oct 13 '22 23:10

Suraj Rao


Pipes are what is required for operator(s) going forward.

version: rxjs 6.0.1

Example:

import { Observable } from "rxjs";
import { map } from "rxjs/operators";

Observable.create((observer: any) => {
    observer.next('Hello')
}).pipe(map((val: any) => val.toUpperCase()))
  .subscribe((x: any) => addItem(x))


function addItem(val: any) {
    console.log('val', val);
}

//output - (In uppercase)
HELLO
like image 25
Dash Avatar answered Oct 13 '22 23:10

Dash


Run these 2 commands after running ng update. This should fix the rxjs imports:

  1. npm i -g rxjs-tslint
  2. rxjs-5-to-6-migrate -p src/tsconfig.app.json

References:

  • https://github.com/ReactiveX/rxjs-tslint
like image 38
user8152243 Avatar answered Oct 13 '22 21:10

user8152243