Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import rxjs timer in angular 6?

I tried importing rxjs timer on my angular 6 project like

import { timer } from 'rxjs/observable/timer';

I also tried it like,

Rx.Observable.timer(200, 100)

They don't work

Here is the code on plunker

like image 933
anonymous Avatar asked May 08 '18 08:05

anonymous


People also ask

What is of() RxJS?

RxJS' of() is a creational operator that allows you to create an RxJS Observable from a sequence of values. According to the official docs: of() converts the arguments to an observable sequence. In Angular, you can use the of() operator to implement many use cases.

What is Rjx in Angular?

RxJS (Reactive Extensions for JavaScript) is a library for reactive programming using observables that makes it easier to compose asynchronous or callback-based code.

Why do we use RxJS in Angular?

The RxJS library is great for handling async tasks. It has a large collection of operators in filtering, error handling, conditional, creation, multicasting, and more. It is supported by JavaScript and TypeScript, and it works well with Angular.


2 Answers

From rxjs 6 (as used in angular 6 project), The general rule is as follows:

  • rxjs: Creation methods, types, schedulers and utilities

    import { timer, Observable, Subject, asapScheduler, pipe, of, from,
             interval, merge, fromEvent } from 'rxjs';
    
  • rxjs/operators: All pipeable operators:

    import { map, filter, scan } from 'rxjs/operators';
    

Here is the migration guide: https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#observable-classes

like image 187
ashfaq.p Avatar answered Oct 08 '22 18:10

ashfaq.p


All observable classes (https://github.com/ReactiveX/rxjs/tree/5.5.8/src/observable) have been removed from v6, in favor of existing or new operators that perform the same operations as the class methods.

import { timer } from 'rxjs';
import { timeInterval, pluck, take} from 'rxjs/operators';

var sourcef = timer(200, 100)
  .pipe(
    timeInterval(),
    pluck('interval'),
    take(3)
  )

Forked Example

See also

  • https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#observable-classes
like image 40
yurzui Avatar answered Oct 08 '22 20:10

yurzui