Use an RxJS Observable in Svelte

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet

Svelte can easily import RxJS and use all the Observables available fairly intuitively. Once you create your new Svelte project, simply install RxJS, create Observables as you would normally, then reference the Observable inside of your HTML with the $ binding to subscribe to it.

This lesson walks you through the process of using an RxJS interval with Svelte by importing RxJS, then creating an interval Observable which we use inside of the template.

Instructor: [0:00] I created a new Svelte app from the default template. We will install RxJS. Inside of our script tag, we can import anything from RxJS. I will import interval. Then I can create a variable called timer, and create a new interval that will fire every one second.

[0:21] All that's left to do inside of our HTML is to use the Svelte bindings to reference our variable, which is an observable. Hit save, and you'll see our timer will start counting.

[0:35] We can also import any of the operators. I want to import the map operator from RxJS/operators. That way, I can pipe the interval into a map, which will take the value and multiply it by two. Now when I hit save, you'll see each value will be multiplied by two.