OOZOU
Get in Touch
Back to Blog

Simple Flux architecture with Vue.js

Using EventBus in Vue.js for a simple flux architecture.

December 12, 2018
2 min read
Simple Flux architecture with Vue.js

For a small app that only needs a simple state management pattern. I always use one component to hold the state of the app, a container. This act as a single source of truth for an app to refer to its states. Vue provides a mechanism to create a global event listener called EventBus which is a super simple way to mimic flux architecture.

All you need is one module that exports Vue as EventBus like so

import Vue from 'vue'
export const EventBus = new Vue()
lang-javascript

Then you can use it inside your container to listen to event from any component inside the app. I normally add the listener on created life cycle.

/*
In this scenario you could just use `vm.$emit` to communicate to `Container` from `CustomButton.
In reality, this pattern will shine when your component is nested inside some more components and
you can't really emit event to the `Container` quite easily.
*/
<template>
<custom-button label='Click me and the I will update the container state' />
</template>

<script>
import { EventBus } from "./EventBus.js"
import CustomButton from "./CustomButton.vue"
export default {
 data () {
  return { buttonClickCount: 0 }
 },
 created () {
  EventBus.$on('my-custom-event', (params) => {
   // you can submit data via params when emitting event.
   console.log(params) 
   this.buttonClickCount++ 
   }
 },
 components: {
  'custom-button': CustomButton
 }
}
</script>
lang-vue

Then you can import EventBus into any of your components and you can triggers my-custom-event from anywhere without having to emit the event to that component’s parent and repeat the whole process in every component in between the component that triggers the change and the target container.

Here is what CustomButton could look like

<template>
<button @click='increaseUpdateCount'>{{label}}</button>
</template>
<script>
import { EventBus } from './EventBus.js'
export default {
 props: ['label'],
 methods: {
   increaseUpdateCount () {
    EventBus.$emit('my-custom-event', 'hello')
   }
  }
}
</script>
lang-vue

That’s it. As your app gets larger, you might want to consider using Vuex especially if many containers shared certain states.

Cheers!

Related Articles

How Can I Design a Software: A Beginners Guide for Business Owners

How Can I Design a Software: A Beginners Guide for Business Owners

Designing software is an exciting yet complex process that involves creativity, logical thinking, and problem-solving skills.

Read More →
The Future of Edge Computing: What Businesses Need to Know

The Future of Edge Computing: What Businesses Need to Know

The digital landscape is evolving rapidly, and businesses must adapt to keep up with rising demands for speed, efficiency, and real-time processing.

Read More →
The Future of Retail: AI, AR, and Digital Transformation Trends

The Future of Retail: AI, AR, and Digital Transformation Trends

Retail is evolving at an unprecedented pace, driven by AI (Artificial Intelligence), AR (Augmented Reality), and digital transformation technologies, revolutionizing the retail industry. Traditional brick-and-mortar stores are no longer just about selling products—they are becoming smart, interactiv

Read More →

Have a Project in Mind?

Let's discuss how we can help bring your ideas to life with our expertise in web and mobile development.

Start a Conversationhello@oozou.com
OOZOU Logo

Bangkok · Singapore · Hong Kong

X
Facebook
Instagram
LinkedIn

Services

  • Web Development
  • AI Agents & Generative AI
  • Mobile App Development
  • Data Analytics & Engineering
  • UI/UX & Product Design
  • Digital Transformation

Company

  • About Us
  • Careers
  • Blog
  • Whitepapers
  • Case Studies
  • Today I Learned
  • Contact

More

  • Industries
  • Get an Estimate
  • Partner Network
© 2026 OOZOU. All rights reserved.
Privacy PolicyCode of ConductABAC Policy