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:
- The documentation from the Ng-book is outdated.
- The documentation from the angular guide is different.
- I open an Issue on GitHub, but they send me back here
fromEvent
I dont have the problem with themap
operator. But thanks, I will check that answer. – Expander