Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 upgrade: debounceTime is not property of Subject

I am attempting to upgrade my app from Angular 5 to Angular 6. I followed the steps on the https://update.angular.io/ At least i think i did.

The Error is :

Property 'debounceTime' does not exist on type 'Subject<string>'.

Also my components lost the debounceTime import. I think the ng update removed it.

like image 421
Logan_B Avatar asked May 05 '18 15:05

Logan_B


People also ask

How do I use debounceTime in angular 6?

Angular 6 onwards, debounceTime is imported as following. import { debounceTime } from 'rxjs/operators'; It is used with pipe operator of Observable . debounceTime is useful in operation where user changes inputs frequently such as search operation.

What is debounceTime in angular?

DebounceTime & Debounce are the Angular RxJs Operators. Both emit values from the source observable, only after a certain amount of time has elapsed since the last value. Both emit only the latest value and discard any intermediate values.

How does debounce time work?

debounceTime delays the values emitted by a source for the given due time. If within this time a new value arrives, the previous pending value is dropped and the timer is reset. In this way debounceTime keeps track of most recent value and emits that most recent value when the given due time is passed.


Video Answer


2 Answers

I solved it with the help of @Siva636 and @Andrew Lobban.

I needed to use pipe:

  this.field$.pipe(
      debounceTime(400),
      distinctUntilChanged())
like image 143
Logan_B Avatar answered Oct 21 '22 10:10

Logan_B


As of Angular 6.1.8, just need to import and add pipe like below example

import {debounceTime} from 'rxjs/operators';

Then on field just before the observable subscribe method call pipe(debounceTime(1000)) like below:

emailControl.valueChanges.pipe(debounceTime(1000)).subscribe(value => this.setMessage(emailControl));

And that's it, it's just a updated answers of @tiago's answer - where she is defining const debouncetime - we don't really need to use const and pipe.

like image 42
Heena Manglani Avatar answered Oct 21 '22 11:10

Heena Manglani