Movatterモバイル変換


[0]ホーム

URL:


Report this

What is the reason for this report?

Integrating RxJS with Vue.js

Published on January 16, 2017
Joshua Bemenderfer

ByJoshua Bemenderfer

Integrating RxJS with Vue.js

If you come from Angular-land or are a fan of functional programming, you’re probably pretty familiar with the concept of observable streams, as implemented most commonly in JS-land by RxJS. Here’s a quick guide on using RxJS with Vue.

Installation

Now, Vue doesn’t come withRxJS support out-of-the-box like other frameworks might, but it does provide an official plugin,vue-rx that adds the needed bindings.

To use it, installvue-rx andrxjs via Yarn or NPM.

# Yarn$yarnadd vue-rx rxjs# NPM$npminstall vue-rx rxjs--save

In your app, you can then use the plugin by importingRxJS and registering the plugin with Vue usingVue.use()

importVuefrom'vue';importRxfrom'rxjs/Rx';importVueRxfrom'vue-rx';// VueRx can use libraries other than RxJS// that implement the observable interface.Vue.use(VueRx,Rx)

Automatically-Managed Subscriptions

The most commonly used feature of Rx-framework integrations is usually the ability to have components automatically subscribe and unsubscribe fromObservables when the component is created and destroyed. This avoids the memory leakages that commonly occur when subscribing to them manually.

To do this withvue-rx, simply add asubscriptions object to your component that maps parameters to yourObservables. The end result is comparable to Angular 2’sasync pipe.

MyComponent.vue
<template><p>{{message$}}</p></template><script>importRxfrom'rxjs/Rx';const messageObservable=Rx.Observable.from(['Example Message','Example Message Final']);exportdefault{subscriptions:{message$: messageObservable}}</script>

Themessage$ variable should now contain the valueExample Message Final and be rendered in your template. The subscription will be automatically destroyed when the component unmounts.

The observable can be accessed inside the component throughthis.$observables.message.

As with the Angular 2 community, in Vue projects it is generally expected that variables that are observable subscriptions are suffixed with a dollar sign ($).

Other Features

Unique Observables

In the event that you’d like to subscribe to different observables for each instance of a component, you can instead set thesubscriptions property to a function that returns an object mapping data properties to observables, as shown below:

exportdefault{subscriptions(){return{message$: messageObservable}}}

Manual Subscription

You can subscribe to an observable inside of a component while still lettingvue-rx handle unsubscribing by usingthis.$subscribeTo(Observable, callback).

exportdefault{mounted(){this.$subscribeTo(messageObservable,function(val){console.log(val)})}}

Links

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Joshua Bemenderfer
Joshua Bemenderfer
Author
Category:
Tags:
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?


This textbox defaults to usingMarkdown to format your answer.

You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to ourPrivacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.

© 2025 DigitalOcean, LLC.Sitemap.

[8]ページ先頭

©2009-2025 Movatter.jp