Vue.JS for a Micro Frontend approach
Feasibility: Composite UI
- Is it possible to create a composite UI (micro front end) based on vue by using standard vue tools?
Yes, it is possible. Pretty much any independent Vue-component you see published around (vue-select, vue-slider-component and even full "sets" of components such as vuetify, bootstrap-vue or vue-material are examples of reusable (composable) components developed using standard Vue tools.
Page transistions: Routing
- We have more than one page, so we need a solution to navigate from one side to another. How can we realize page transitions?
vue-router is the tool for this job. It is developed by the core team, so expect tight integration and great feature support.
Event-Bus
- Is it possible to established a Event-Bus between the VueJS components?
Every Vue instance implements an events interface. This means that to communicate between two Vue instances or components you can use Custom Events. You can also use Vuex (see below).
Bidirectional communication
- How can we implement a bidirectional communication between the components?
The best way to send data from parent component to child is using props.Steps:
- Declare
props
(array or object) in the child - Pass it to the child via
<child :name="variableOnParent">
.
See demo below:
You can also get references for Child Components (ref
s) and call methods on them.
To communicate from child to parent, you'll use events. See demo below. There are also several modifiers that make this task easier.
Vuex
Inevitably, though, as your application grows, you will have to use a more scalable approach. Vuex is the de facto solution in this case. Roughly, when using Vuex, you won't have to pass state around from parent to child: all of them will pick it up from the Vuex store (sort of a "global" reactive variable). This greatly simplifies the application management and is worth a post of its own.
Final note: As you can see, one great advantage of Vue is how easy you can prototype and test functionality. No build step, few abstractions over raw JS. Compared to other frameworks, I'd say this is an important bonus.
I have been curious looking for a quick way to implement the Micro-Frontend architecture. Some good resources I found are at:
- micro-frontends.org: techniques, strategies and recipes for building a modern web app with multiple teams that can ship features independently.
- Single-SPA.js: Javascript router for front-end microservices
However the problem I had with them is setup complexity. I like to see results pretty fast.
Piral
Piral.io (framework for portal-applications following microfrontends architecture) is still pretty much in development and it's mainly targeted for React (another JS frontend-framework).
Vue approach
I was able to come up with an approach and hope to write an medium article on that soon, however for the time being
- You can build each part of your application as an independent Web Component with Vue. A great place to start is Create & Publish Web Components With Vue CLI 3 - Vue.js Developers.
- You can use vue-router for page-transitions: with dynamic route matching (e.g
/apps/:app_name
) you can load sub-applications as appropriate. Within each sub-app, you can as well have a routing system in place - As Event-Bus there's BroadcastChannel in recent browsers, which can be used to send messages across sub-apps. The Broadcast Channel API could be resourceful.
- BroadcastChannel can handle bi-directional communication.
Above approach works best if you want to:
- Have separate team use whatever tool they're most comfortable with.
- Add new apps, even in production without downtime.