Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Watch Vue.js v-models as Observable with $watchAsObservable and RxJS

    5.5.12 - 6.4.0

    You most likely already have data or properties in your template which are controlled by third-party components or updated using data binding. You can still use this data as stream by leveraging vue-rx's $watchAsObservable then chaining RxJS operators onto it as a new stream.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: You probably already have some components with some V models. For example, I'll create some data, and we'll return an active tab and set that to zero.

    Say you're using that as the V model of a tabs component, so this is the Bulma tabs component from Buefy, which has V tab items. V tab items have labels, so we'll set this label to Luke, that one to Darth, and that one to Leia, so Darth, Leia. Then set the V model here to active tab.

    Then just render out the active tab above the tab component. If I click Darth, this changes to one, and Leia, this changes to two. I can watch this V model value as an observable using this.$watchAsObservable, and then a string of active tab. We'll call this our active tab stream.

    We can return that and our object, active tab, and now render out our active tab stream. Now you'll notice there's no value there. If I click on Darth, it changes to an object of old value and new value. Leia, old one's Darth, new one's Leia, but if I refresh the page, that value disappears.

    We can trigger that to fire right away in here with some options. These are the same options as the $watch of view, and one of them is called immediate. We'll set that to true. That means to fire right away. You can see we get a new value of zero, and I didn't have to click anything. This still works, that still works, and that still works.

    You often don't have to worry about the old values. We can go ahead and pluck the new value, hit save, and now this zero up here is an observable stream, which updates whenever my component V model updates.