Piral Vue 3 Plugin
piral-vue-3 is a plugin designed to seamlessly integrate Vue 3 components within a Piral microfrontend application. It enables developers to build 'pilets' (microfrontends) using Vue 3, which can then be dynamically loaded and rendered by a Piral instance (the 'shell' application). The current stable version is 1.10.3, with minor point releases occurring frequently, typically addressing dependency updates, bug fixes, and improvements across the Piral ecosystem. Key differentiators include its tight integration with the Piral Pilet API, allowing Vue 3 components to leverage the Piral communication layer and shared services, and its focus on enabling a multi-framework approach within a single microfrontend host. It abstracts away the complexities of mounting and unmounting Vue 3 applications within the Piral lifecycle, providing a robust solution for incorporating Vue 3 into larger, heterogeneous microfrontend architectures.
Common errors
-
Error: Hydration completed but contains mismatches.
cause This error typically occurs in SSR (Server-Side Rendering) scenarios where the client-side rendered content doesn't exactly match the server-generated HTML, often due to dynamic content or client-only side effects.fixEnsure that your Vue 3 pilet and the Piral shell handle SSR correctly. If using `piral-vue-3` in an SSR context, make sure data fetching is completed before hydration and that client-specific code runs only in the browser environment. For client-only rendering, this error might indicate late-loaded content that causes DOM changes. -
Cannot read properties of undefined (reading '$root')
cause This error often indicates that a Vue component is trying to access `$root` or other instance properties that might not be available in a non-Vue context or when the component is not properly mounted or provided with a root instance.fixVerify that your Vue 3 component is correctly registered and mounted by the `piral-vue-3` plugin. Ensure that any Piral-specific API extensions (e.g., `app.foo()`) are accessed through the `PiletApi` provided to the `setup` function and not via Vue's internal mechanisms unless explicitly passed down. -
Failed to resolve component: [ComponentName]
cause The Vue 3 component specified in `app.registerPage` or `app.registerTile` could not be found or imported correctly within the pilet's context.fixDouble-check the import path and name of your Vue component. Ensure it's correctly exported from its module and that your pilet's build process correctly bundles it. If using lazy loading, verify the dynamic import syntax and path.
Warnings
- gotcha When developing Vue 3 pilets, ensure that your pilet's `package.json` correctly defines `vue` as a peer dependency and that the Piral shell provides it. Mismatched Vue versions between the shell and pilet, or a pilet bundling its own Vue runtime when it shouldn't, can lead to runtime errors or increased bundle sizes.
- breaking The `piral-vue-3` plugin is specifically designed for Vue 3. Using it with Vue 2 projects or attempting to mix Vue 2 and Vue 3 pilets without explicit support or isolation mechanisms can lead to runtime failures due to incompatible APIs and rendering contexts.
- gotcha Vue 3 components registered via `piral-vue-3` operate within the Piral application's lifecycle. Improper cleanup of Vue instances or event listeners within a pilet's `teardown` function can lead to memory leaks, especially when pilets are dynamically loaded and unloaded frequently.
Install
-
npm install piral-vue-3 -
yarn add piral-vue-3 -
pnpm add piral-vue-3
Imports
- createVue3Api
const { createVue3Api } = require('piral-vue-3');import { createVue3Api } from 'piral-vue-3'; - Vue3PiletApi
import { Vue3PiletApi } from 'piral-vue-3';import type { Vue3PiletApi } from 'piral-vue-3'; - defineVue3Pilet
import defineVue3Pilet from 'piral-vue-3';
import { defineVue3Pilet } from 'piral-vue-3';
Quickstart
import { Piral, createPiralFactory } from 'piral';
import { createVue3Api } from 'piral-vue-3';
import { createApp, defineComponent } from 'vue';
const MyVueComponent = defineComponent({
template: '<div>Hello from Vue! Count: {{ count }}</div>',
data() {
return { count: 0 };
},
mounted() {
setInterval(() => {
this.count++;
}, 1000);
}
});
const piralApp = createPiralFactory({
requestPilets() {
return Promise.resolve([
{
name: 'my-vue-pilet',
version: '1.0.0',
spec: 'v1',
hash: 'abc',
dependencies: {},
setup(app) {
app.registerPage('/vue-page', MyVueComponent);
app.showNotification('Vue Pilet Loaded!', { autoClose: 2000 });
}
}
]);
},
plugins: [createVue3Api()]
});
// In a real Piral application, the Piral instance would be mounted to a DOM element.
// For demonstration, we'll just show the setup.
// Piral renders its components into a designated DOM container.
// For a full Piral app, this would be `render(<Piral instance={piralApp} />, document.querySelector('#app'))`.
console.log('Piral app with Vue 3 plugin initialized.', piralApp);
// Simulate a Piral app mounting cycle if needed for full execution context.
// In a browser environment, you'd mount `<Piral instance={piralApp} />` into your root element.