Vue RxJS Bindings

6.2.0 · active · verified Tue Apr 21

Vue-Rx is an official integration library that provides RxJS bindings for Vue.js components, enabling developers to incorporate reactive programming paradigms directly into their Vue applications. It offers a `subscriptions` option for components to declaratively bind observable streams to data properties, and a `v-stream` directive to easily convert DOM events or custom component events into RxJS Observables. The current stable version, 6.2.0, primarily supports RxJS v6 and Vue.js 2.x, with releases focused on compatibility updates and feature enhancements. Its key differentiator is its first-party status within the Vue ecosystem, offering a streamlined, boilerplate-reducing approach to managing reactive state and event handling using RxJS, and it ships with TypeScript typings for enhanced developer experience.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `vue-rx` setup, registering the plugin, defining a reactive `count` property using the `subscriptions` option, and utilizing `v-stream` to link a button click event to an RxJS Subject for real-time updates.

import Vue from 'vue';
import VueRx from 'vue-rx';
import { Subject } from 'rxjs';
import { map, startWith, scan } from 'rxjs/operators';

// Install VueRx as a Vue plugin
Vue.use(VueRx);

const app = new Vue({
  el: '#app',
  // Define reactive subscriptions for component data
  subscriptions() {
    // Create a Subject to stream click events
    this.plus$ = new Subject<{ event: MouseEvent, data?: any }>();

    return {
      // Bind 'count' data property to an observable stream
      count: this.plus$.pipe(
        map(() => 1), // Each click adds 1
        startWith(0), // Initial value is 0
        scan((total, change) => total + change) // Accumulate total
      )
    };
  },
  template: `
    <div>
      <h2>Counter: {{ count }}</h2>
      <!-- Use v-stream to pipe DOM click events into the 'plus$' Subject -->
      <button v-stream:click="plus$">Increase Count</button>
      <button @click="console.log('Resetting state often requires another stream or logic.')">Reset (concept)</button>
    </div>
  `
});

view raw JSON →