简体   繁体   中英

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

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

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

as of rxjs 6.2.2, for this import

import { timer } from 'rxjs';   // gives tslint blacklisted error

tslint gives an error:

ERR: [tslint] This import is blacklisted, 
import a submodule instead (import-blacklist)

but this works fine without any error

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

In my case I used import { timer } from 'rxjs/Observable/timer'; like this.

but need to use in import { timer } from 'rxjs/observable/timer'; Observable instead of observable.

that's all... enjoy your coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM