switchMap not working with string observable. If I use it inside a function then call the function, it won't work
Below is the stackblitz link to code: https://stackblitz.com/edit/typescript-btkudb?file=index.ts
switchMap not working with string observable. If I use it inside a function then call the function, it won't work
Below is the stackblitz link to code: https://stackblitz.com/edit/typescript-btkudb?file=index.ts
You seem to have already imported RxJS fromEvent
function but instead opted to use addEventListener()
. I'd say fromEvent
suits better here since you're trying to pipe in RxJS operators to the event. You could try something like
import { interval, fromEvent, of } from "rxjs";
import { map, tap, switchMap } from "rxjs/operators";
fromEvent(<HTMLElement>document.querySelector("#app"), "click")
.pipe(
tap(_ => console.log("clicked")),
map(_ => of("test")), // <-- serves no purpose?
switchMap(() => interval(1000))
)
.subscribe(console.log);
I've modified your Stackblitz.
Working example:
var { interval, fromEvent, of } = rxjs;
var { map, tap, switchMap } = rxjs.operators;
fromEvent(document.querySelector("#app"), "click").pipe(
tap(_ => console.log("clicked")),
map(_ => of("test")), // <-- serves no purpose?
switchMap(() => interval(1000))
).subscribe(console.log);
<script src="https://unpkg.com/rxjs@6.4.0/bundles/rxjs.umd.min.js"></script>
<div id="app">abcd</div>
Moreover, while I appreciate the working example in the question, it's always better to write the code in the question instead of only linking to an external website. It ensures question's longevity.