Javascript

Introduction to VueJS and RxJS

Adesoji Temitope
3 min read
Introduction to VueJS and RxJS
This article was written over 18 months ago and may contain information that is out of date. Some content may still be relevant, but please refer to official documentation for the latest information.

Let's start with a brief introduction.

What is VueJS

Vue.js is a progressive open-source front end JavaScript framework for building user interfaces, and single-page applications.

What is RxJS

RxJS is a library for reactive programming, and has components that enable the composition of asynchronous code. This means it takes data as streams (Observables) that can be subscribe to.

Installation/Setup

Following these steps, let's set-up our Vue application, and install RxJS:

Vue create <AppName>

yarn i rxjs

yarn serve

Creating an Observable

To create an observable

  new Observable(function subscribe(subscriber) {});

An observer of an observable is an object with three functions: next, error, & complete.

observable.subscribe({
  next: value => console.log(value),
  error: err => console.log(err),
  complete: () => console.log(`Completed`),
})

Let's take a look at an example to better understand Observables.

<script lang="ts">
import { defineComponent, onMounted, onBeforeUnmount } from "vue";
import { Observable, Subscription} from "rxjs";

export default defineComponent({
  setup() {
    const time = 2500;
    let observableID: Subscription
    onMounted(() => {
      const observable$ = new Observable(function subscribe(subscriber) {
        const intervalId = setInterval(() => {
          subscriber.next("Vuejs and Rxjs");
          subscriber.complete();
          clearInterval(intervalId);
        }, time);
      });
      observableID = observable$.subscribe(
        (value) => console.log(`Introduction to ${value}`),
        (err) => console.log(err),
        () => console.log("completed")
      );
    });
    onBeforeUnmount(() => observableID.unsubscribe()); 
  },
});
</script>

From the example above, we can see that “Introduction to Vuejs and Rxjs” is printed out after 2.5 seconds, and then "completed" is printed after.

RxjS operators

Creating an observable manually every time can make code become very lengthy and difficult to read. Therefore, RxJS has alot of useful operators.

Some of the most commonly used operators are

  • Creation Operators
  • Transformation Operators
  • Filtering Operators
  • Combination Operators
  • Conditional Operators
  • Join Operators
  • Multicasting Operators
  • Error Handling Operators

We would be discussing examples on Creation, Transformation, and Filtering operators in this post:

Creation operators

These operators make creating an observable easy for various usecase. Some examples are 'interval', 'from', and 'of'.

  • interval: Creates an observable that emits sequential numbers every specified interval of time.
  interval(10).subscribe(console.log);
  • from: Creates an observable from an array, promise, iterables or string
const frameworks = of("VueJS", "ReactJS", "Svelte", "AngularJS", "Lit", "RiotJS").subscribe(val => console.log(val));

//output: "VueJS", "ReactJS", "Svelte", "AngularJS",  "Lit", "RiotJS",

const promiseSource = from(new Promise(resolve => resolve('Hello World!'))).subscribe(val => console.log(val));

//output: 'Hello World!'
  • of: Creates an observable from a sequence of values
const frameworks = of("VueJS", "ReactJS", "Svelte", "AngularJS", "Lit", "RiotJS").subscribe(val => console.log(val));

//output: "VueJS", "ReactJS", "Sevelte", "AngularJS",  "Lit", "RiotJS"

Transformation operators

These operators provide data transformation techniques for values passing through.

An example is 'map'. For the example below, we use map to transform our array of objects ([{name: "VueJS", language: "js"}]) into an array of strings(["VueJS"]).

    const source = [
      { name: "VueJS", language: "js" },
      { name: "ReactJS", language: "js" },
      { name: "Laravel", language: "PHP" },
      { name: "Sevelte", language: "js" },
      { name: "AngularJS", language: "js" },
      { name: "Spring", language: "java" },
      { name: "Lit", language: "js" },
      { name: "CodeIgniter", language: "PHP" },
      { name: "RiotJS", language: "js" },
    ];

    from(source).pipe(map(({ name }) => name));
    jsFrameworks.subscribe((value) => {
      frameworks.value.push(value);
    });

Filtering Operator

This operator helps in choosing and refining how and when data is obtained from an observable.

An example is "Filter". For the example below, we use "filter" to obtain an array of objects where language is "js".

    const source = [
      { name: "VueJS", language: "js" },
      { name: "ReactJS", language: "js" },
      { name: "Laravel", language: "PHP" },
      { name: "Svelte", language: "js" },
      { name: "AngularJS", language: "js" },
      { name: "Spring", language: "java" },
      { name: "Lit", language: "js" },
      { name: "CodeIgniter", language: "PHP" },
      { name: "RiotJS", language: "js" },
    ];
    from(source).pipe(
      filter(({ language }) => language === "js"),
      map(({ name }) => name)
    );

Example

Now that we have discussed some of the basics, lets try building a page that shows a list of frameworks. Each item of the list should be delayed before being displayed.

<template>
  <div>
    Test Rxjs
    <h2>Filter Frameworks</h2>
    <ul>
      <li v-for="item in frameworks" :key="item">{{ item }}</li>
    </ul>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from "vue";
import { interval } from "rxjs";
import { map, filter } from "rxjs/operators";

export default defineComponent({
  setup() {
    let frameworks = ref([]);
    const time = 1000;

    const source = [
      { name: "VueJS", language: "js" },
      { name: "ReactJS", language: "js" },
      { name: "Laravel", language: "PHP" },
      { name: "Svelte", language: "js" },
      { name: "AngularJS", language: "js" },
      { name: "Spring", language: "java" },
      { name: "Lit", language: "js" },
      { name: "CodeIgniter", language: "PHP" },
      { name: "RiotJS", language: "js" },
    ];

    onMounted(() => {
      const jsFrameworks = interval(time).pipe(
        filter((i) => source[i].language === "js"),
        map((i) => source[i].name)
      );
      const observable$ = jsFrameworks.subscribe(
        (value) => {
          frameworks.value.push(value);
        },
        () => observable$.unsubscribe()
      );
    });
    return {
      frameworks,
    };
  },
});
</script>

Live Demo

This is a codesandbox demo for you to play around with:

Conclusion

RxJS is really expansive, and can't be covered in just a single blog post. To learn more about RxJs, checkout the official documentation here: https://rxjs.dev/ or https://www.learnrxjs.io/. There is also a very good Vue plugin for Rxjs here.

If you have any questions or run into any trouble, feel free to reach out on Twitter or Github.

About the author

Adesoji Temitope

Adesoji Temitope

Software Engineer

Keep reading

View all posts →

Awesome 3D experience with VueJS and TresJS: a beginner's guide

Unleash the power of 3D in your Vue.js projects with Tres.js! The future of immersive web experiences is here. #Vuejs #3Ddevelopment...

Mattia Magi5 mins
Vue3D

3 VueJS Component Libraries Perfect for Beginners

For developers checking out VueJS for the first time, the initial steps are overwhelming, particularly when setting up projects from square one. But don’t worry! The VueJS ecosystem offers a plethora of remarkable component libraries, easing this early obstacle. These three libraries are pre built toolkits, providing beginners with the means to kickstart their VueJS projects effortlessly. Let’s take a look! Quasar Quasar is among the most popular open source component libraries for Vue.js, offering a comprehensive set of ready to use UI components and tools for building responsive web applications and websites. Designed with performance, flexibility, and ease of use in mind, Quasar provides developers with a wide range of customizable components, such as buttons, forms, dialogs, and layouts, along with built in support for themes, internationalization, and accessibility. With its extensive documentation, active community support, and seamless integration with Vue CLI and Vuex, Quasar empowers developers to rapidly prototype and develop high quality Vue.js applications for various platforms, including desktop, mobile, and PWA (Progressive Web Apps). PrimeVue PrimeVue is a popular Vue.js component library offering a wide range of customizable UI components designed for modern web applications. Developed by PrimeTek, it follows Material Design guidelines, ensuring responsiveness and accessibility across devices. With features like theming, internationalization, and advanced functionalities such as lazy loading and drag and drop, PrimeVue provides developers with the tools to create elegant and high performing Vue.js applications efficiently. Supported by clear documentation, demos, and an active community, PrimeVue is an excellent choice for developers seeking to streamline their development process and deliver polished user experiences. Vuetify Vuetify is a powerful Vue.js component library that empowers developers to create elegant and responsive user interfaces with ease. Built according to Google's Material Design guidelines, Vuetify offers a vast collection of customizable UI components, ranging from buttons and cards to navigation bars and data tables. Its comprehensive set of features includes themes, typography, layout grids, and advanced components like dialogues and sliders, enabling developers to quickly build modern web applications that look and feel polished. With extensive documentation, active community support, and ongoing development, Vuetify remains a top choice for Vue.js developers seeking to streamline their workflow and deliver visually stunning user experiences. For newcomers venturing into Vue.js, the initial setup might seem daunting. Thankfully, Vue.js offers a variety of component libraries to simplify this process. Quasar, PrimeVue, and Vuetify are standout options, each providing pre built tools to kickstart projects smoothly. Whether you prefer Quasar's extensive UI components, PrimeVue's Material Design inspired features, or Vuetify's responsive interfaces, these libraries cater to diverse preferences and project requirements. With their clear documentation and active communities, these libraries empower developers to start Vue.js projects confidently and efficiently, enabling Vue developers to create polished user experiences....

2 mins
VuetifyVueQuasar

Understanding Vue.js's <Suspense> and Async Components

In this blog post, we will delve into how <Suspense> and async components work, their benefits, and practical implementation strategies to make your Vue.js applications more efficient and user-friendly...

Jesús Padrón6 mins
VueWeb PerformanceUXJavaScript

Getting started with Vitepress

Create your static blog site using Vitepress. Setup and configure your site in minutes by following this step-by-step tutorial....

Simone Cuomo12 mins
VueViteVitepress