Piral Vue 2 Plugin
Piral-vue is a plugin designed for integrating Vue 2 components within a Piral microfrontend application. It acts as a converter, allowing developers to seamlessly register and render existing or new Vue 2 components as pilets (microfrontends) or parts of pilets. The package is an integral part of the larger Piral ecosystem, which focuses on building scalable and maintainable web portals using a microfrontend architecture, facilitating the combination of different frontend frameworks. As of version 1.10.3, it continues to receive regular updates, typically with minor point releases addressing bug fixes, dependency updates, and improvements within the broader Piral framework, ensuring compatibility and stability within the Piral ecosystem. Its key differentiator is its explicit support for Vue 2, enabling the reuse of established Vue 2 codebases within a modern microfrontend shell. This provides a clear migration path or coexistence strategy for organizations with significant investments in Vue 2 applications, allowing them to incrementally modernize or integrate without forcing an immediate, large-scale upgrade to Vue 3. The plugin handles the necessary lifecycle management and integration points to make Vue 2 components behave as native Piral extensions or pages.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'extend')
cause This error typically occurs when a Vue 3 component or API is mistakenly used within a Vue 2 context (or vice versa), or when Vue 2 is not properly loaded/available. The `extend` method is specific to Vue 2's component definition.fixVerify that `vue@^2.x` is correctly installed in your pilet's `package.json` and that your component definitions strictly adhere to the Vue 2 API. If you intend to use Vue 3, switch to `piral-vue3` instead of `piral-vue`. -
Failed to mount component: template or render function not defined.
cause This common Vue 2 error indicates that the component object passed to `app.fromVue()` is missing either a `template` string or a `render` function, both of which are essential for Vue to render the component.fixEnsure your Vue 2 component definition includes either a `template` property (as a string) or a `render` function. Double-check for any syntax errors in your component's options object or if a build step (like a Vue loader) failed to process the component correctly.
Warnings
- breaking This `piral-vue` package is strictly designed for Vue 2.x applications. Attempting to use it with Vue 3.x or newer will result in runtime errors due to significant API changes between Vue major versions.
- gotcha Vue components registered through `piral-vue` operate within isolated contexts. Relying on global Vue instances or directly modifying globally shared state (e.g., a single Vuex store) across multiple pilets can lead to unpredictable behavior, state collisions, or memory leaks.
- gotcha While Vue components often use scoped CSS, integration into a microfrontend shell can still lead to styling conflicts. Global styles from the Piral shell or other pilets might unintentionally affect Vue 2 components, and vice-versa.
Install
-
npm install piral-vue -
yarn add piral-vue -
pnpm add piral-vue
Imports
- createVueApi
const createVueApi = require('piral-vue');import { createVueApi } from 'piral-vue'; - PiletVueApi
import { PiletVueApi } from 'piral-vue';import type { PiletVueApi } from 'piral-vue'; - VueComponent
import { VueComponent } from 'vue';import type { VueComponent } from 'piral-vue';
Quickstart
import type { PiletApi } from 'piral';
import { defineComponent } from 'vue'; // Ensure vue@2 is a dependency in your pilet
// Extend PiletApi with piral-vue specific functions provided by the Piral instance.
// This assumes 'piral-vue' has been configured in the Piral instance's createInstance call.
interface MyPiletApi extends PiletApi {
fromVue(component: any): any; // The actual type is VueComponentConverter<VueComponent>
}
// Define a simple Vue 2 component
const MyVueComponent = defineComponent({
name: 'MyVueComponent',
props: {
message: {
type: String,
default: 'Hello from Vue 2 Pilet!',
},
},
template: `
<div style="padding: 10px; border: 1px solid #42b983; margin: 10px; border-radius: 5px;">
<h3>{{ message }}</h3>
<p>This is a Vue 2 component served by a Pilet.</p>
<button @click="increment">Count: {{ count }}</button>
</div>
`,
data() {
return {
count: 0,
};
},
methods: {
increment() {
this.count++;
},
},
});
/**
* The setup function for the Pilet.
* It registers a Vue component as an extension and a page within the Piral application.
*/
export function setup(app: MyPiletApi) {
// Register the Vue component as an extension.
// It can be embedded anywhere in the Piral app using <Extension name="vue-pilet-card" />.
app.registerExtension('vue-pilet-card', app.fromVue(MyVueComponent));
// Register the Vue component as a full page, accessible via a route.
app.registerPage('/vue-pilet-page', app.fromVue(MyVueComponent));
console.log('Vue 2 Pilet: MyVueComponent registered as extension "vue-pilet-card" and page "/vue-pilet-page".');
}