30
votes

I am having a problem trying to create to turn the keyup events into an observable stream.

I am following the Ng-book version 6. I am stuck in an example that makes a search YouTube video as you type. When the search returns we’ll show a list of video thumbnail results, along with a description and link to each video.

So for this, we use an observable.fromEvent: https://github.com/NiLinli/ng-book2-angular-6-r68-code-samples/blob/master/http/src/app/you-tube-search/search-box.component.ts

In RxJS 5, it provides a way to listen to events on an element using Rx.Observable.fromEvent.

ngOnInit(): void {
// convert the `keyup` event into an observable stream
Observable.fromEvent(this.el.nativeElement, 'keyup')

But I am getting this error:

ERROR in src/app/search-box/search-box.component.ts(42,13): error TS2339: Property 'fromEvent' does not exist on type 'typeof Observable'.

I RxJS 6 the import for Observable and fromEvent is like this:

import { Observable, fromEvent } from 'rxjs';

This is my code for the RxJs5 version in Angular 6: (it doesnt work)

Observable.fromEvent(this.el.nativeElement, 'keyup')
.map((e: any) => e.target.value) // extract the value of the input 
.filter((text: string) => text.length > 1) // filter out if empty 
.debounceTime(250) // only once every 250ms 
.do(() => this.loading.emit(true)) // enable loading
      // search, discarding old events if new input comes in
.map((query: string) => this.youtube.search(query)) 
.switch()

This is my try with RxJs 6 and angular 6 (it's updated according with the last answer)

I am trying to use the pipe syntax:

    const obs = fromEvent(this.el.nativeElement, 'keyup')
        .pipe (
            .map((e:any) => e.target.value) // extract the value of the input

            // .filter((text:string) => text.length > 1) //filter out if empty

            .debounceTime(250) //only search after 250 ms

            .tap(() => this.loading.emit(true)) // Enable loading
            // search, call the search service

            .map((query:string) => this.youtube.search(query)) 
            // discard old events if new input comes in

            .switchAll()
            // act on the return of the search
            )   

But I have this error:

Property 'map' doesnt exist on type Observable{<>}

And in the command line, after run ng serve:

ERROR in search-box/search-box.component.ts(40,4): error TS1135: Argument > expression expected. search-box/search-box.component.ts(54,4): error TS1128: Declaration or > statement expected.

But, it is not working... So, how should I write my pipe sintax?

How is my research going on:

  1. The documentation from the Ng-book is outdated.
  2. The documentation from the angular guide is different.
  3. I open an Issue on GitHub, but they send me back here
2
@IngoBürk But I am trying to import my fromEvent I dont have the problem with the map operator. But thanks, I will check that answer.ValRob
how to mock fromEvent now in unit test?Aniruddha Das

2 Answers

43
votes

This should work in your case :

import { fromEvent } from 'rxjs';
import { map, filter, debounceTime, tap, switchAll } from 'rxjs/operators';

  const obs = fromEvent(this.el.nativeElement, 'keyup')
    .pipe (
        map((e:any) => e.target.value), // extract the value of the input

        // filter((text:string) => text.length > 1), //filter out if empty

        debounceTime(250), //only search after 250 ms

        tap(() => this.loading.emit(true)), // Enable loading
        // search, call the search service

        map((query:string) => this.youtube.search(query)) ,
        // discard old events if new input comes in

        switchAll()
        // act on the return of the search
        ) 

Hope it helps !

EDIT Here is a working demo on Stackblitz: Demo Angular 6 + Rxjs 6

11
votes

In RxJS 5, you were writing

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

The imports changed for RxJS 6

import { Observable, of, Subject, Subscription } from 'rxjs';
import { map, filter, debounceTime, distinctUntilChanged } from 'rxjs/operators';

For more information, have a look at the RxJS migration guide.