vue-next-rx only works with RxJS v6+ by default. If you want to keep using RxJS v5 style code, install rxjs-compat.
Installation
NPM + ES2015 or more
rxjs is required as a peer dependency.
npm install vue @nopr3d/vue-next-rx rxjs --save
import Vue from "vue";
import VueRx from "@nopr3d/vue-next-rx";
Vue.use(VueRx);
When bundling via webpack, dist/vue-next-rx.esm.js is used by default. It imports the minimal amount of Rx operators and ensures small bundle sizes.
To use in a browser environment, use the UMD build dist/vue-next-rx.js. When in a browser environment, the UMD build assumes window.rxjs to be already present, so make sure to include vue-next-rx.js after Vue.js and RxJS. It also installs itself automatically if window.Vue is present.
<!-- bind to it normally in templates -->
<!-- on change DOM is update too -->
<div>{{ msg }}</div>
Other API Methods
$watchAsObservable(expOrFn, [options])
This is a prototype method added to instances. You can use it to create an observable from a Data. The emitted value is in the format of { newValue, oldValue }:
<!-- bind to it normally in templates -->
<!-- on change DOM is update too -->
<div>{{ msg }}</div>
<!-- Will display : Old message, after 1 second display "New Message !" -->
<div>{{oldMsg}}</div>
<!-- wait for value and display "Old Message" after 1 second -->
$subscribeTo(observable, next, error, complete)
This is a prototype method added to instances. You can use it to subscribe to an observable, but let VueRx manage the dispose/unsubscribe.
import { interval } from "rxjs";
const vm = new Vue({
mounted() {
this.$subscribeTo(interval(1000), function (count) {
console.log(count);
});
},
});
$fromDOMEvent(selector, event)
This is a prototype method added to instances. Use it to create an observable from DOM events within the instances' element. This is similar to Rx.Observable.fromEvent, but usable inside the subscriptions function even before the DOM is actually rendered.
selector is for finding descendant nodes under the component root element, if you want to listen to events from root element itself, pass null as first argument.
import { pluck } from "rxjs/operators";
const vm = new Vue({
subscriptions() {
return {
inputValue: this.$fromDOMEvent("input", "keyup").pipe(
pluck("target", "value")
),
};
},
});
<div><input /></div>
<div>{{inputValue}}</div>
$createObservableMethod(methodName)
Convert function calls to observable sequence which emits the call arguments.
This is a prototype method added to instances. Use it to create a shared hot observable from a function name. The function will be assigned as a vm method.