Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 28, 2022. It is now read-only.
/vue-next-rxPublic archive

RxJS integration for Vue next

License

NotificationsYou must be signed in to change notification settings

mylabz-xyz/vue-next-rx

RxJS v7 integration forVue next



NOTE

  • vue-next-rx only works with RxJS v6+ by default. If you want to keep using RxJS v5 style code, installrxjs-compat.

Installation

NPM + ES2015 or more

rxjs is required as a peer dependency.

npm install vue @nopr3d/vue-next-rx rxjs --save
importVuefrom"vue";importVueNextRxfrom"@nopr3d/vue-next-rx";Vue.use(VueNextRx);

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 builddist/vue-next-rx.js. When in a browser environment, the UMD build assumeswindow.rxjs to be already present, so make sure to includevue-next-rx.js after Vue.js and RxJS. It also installs itself automatically ifwindow.Vue is present.

Example:

<scriptsrc="https://unpkg.com/rxjs@^7/dist/bundles/rxjs.umd.min.js"></script><scriptsrc="https://unpkg.com/vue@next"></script><scriptsrc="../dist/vue-next-rx.js"></script><divid="app"><divclass="home"><buttonv-stream:click="click$">Click Me</button></div></div><script>const{ Subject, Observable, BehaviorSubject}=rxjs;const{ map, startWith, scan}=rxjs.operators;const{ ref, watch}=VueNextRx;// Use VueNextRxconstapp=Vue.createApp({domStreams:["click$"],subscriptions(){this.click$.pipe(map(()=>"Click Event")).subscribe(console.log);// On click will print "Click Event"},}).use(VueNextRx);app.mount("#app");</script>


Usage


Subscriptions

// Expose `Subject` with domStream, use them in subscriptions functionsexportdefaultdefineComponent({name:"Home",domStreams:["click$"],subscriptions(){return{count:this.click$.pipe(map(()=>1),startWith(0),scan((total,change)=>total+change)),};});
<div><buttonv-stream:click="click$">Click Me</button></div><div>{{count}}</div><!-- On click will show 0, 1 ,2 ,3... -->

Or


// Expose `Subject` with domStream, use them in subscriptions functionsexportdefaultdefineComponent({name:"Home",domStreams:["action$"],subscriptions(){this.action$.pipe(map(()=>"Click Event !")).subscribe(console.log);// On click will print "Click Event"},});

Tips

You can get the data by simply plucking it from the source stream:

constactionData$=this.action$.pipe(pluck("data"));

You can bind Subject by this way

<buttonv-stream:click="action$">Click Me!</button>or<buttonv-stream:click="{ subject: action$, data: someData }">+</button>


Ref

import{ref}from"@nopr3d/vue-next-rx";// use ref like an Rx SubjectexportdefaultdefineComponent({name:"Home",components:{},setup(){constmsg=ref("Message exemple");setTimeout(()=>{msg.value="New message !";},2000);msg.subscribe((value)=>{console.log(value);// After 2s will print : New message !});return{ msg};},});
<!-- bind to it normally in templates --><!-- on change DOM is update too --><div>{{ msg }}</div>


Watch

import{ref,watch}from"@nopr3d/vue-next-rx";exportdefaultdefineComponent({name:"Home",components:{},setup(){constmsg=ref("Message exemple");watch(msg).subscribe((val)=>{console.log(val);// After 2s will print : New message !});setTimeout(()=>{msg.value="New message !";},2000);return{ msg};},});
<!-- 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 }:

import{ref}from"@nopr3d/vue-next-rx";exportdefaultdefineComponent({name:"Home",setup(){constmsg=ref("Old Message");setTimeout(()=>(msg.value="New message incomming !"),1000);return{ msg};},subscriptions(){return{oldMsg:this.$watchAsObservable("msg").pipe(pluck("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 VueNextRx manage the dispose/unsubscribe.

import{interval}from"rxjs";constvm=newVue({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 toRx.Observable.fromEvent, but usable inside thesubscriptions 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, passnull as first argument.

import{pluck}from"rxjs/operators";constvm=newVue({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.

<custom-form:onSubmit="submitHandler"></custom-form>
constvm=newVue({subscriptions(){return{// requires `share` operatorformData:this.$createObservableMethod("submitHandler"),};},});

You can use theobservableMethods option to make it more declarative:

newVue({observableMethods:{submitHandler:"submitHandler$",// or with Array shothand: ['submitHandler']},});

The above will automatically create two things on the instance:

  1. AsubmitHandler method which can be bound to in template withv-on;
  2. AsubmitHandler$ observable which will be the stream emitting calls tosubmitHandler.

example


Example

See/examples for some simple examples.



Test

Test look goods, feel free to open an issue !


Contributors


License

MIT



[8]ページ先頭

©2009-2025 Movatter.jp