Vue.js course ยท Module 14: Directives & Plugins
Plugin Ecosystem and Integration
In this lesson5
In NOVA LAB, every space station module relies on proven, tested systems - from navigation to communication. In the Vue ecosystem, there are popular plugins that solve common problems. Let's explore them and learn how to create advanced plugins.
Popular Vue Plugins
vue-i18n - Internationalization
vue-i18n is the official Vue plugin for multilingual support:
import { createI18n } from 'vue-i18n'
const i18n = createI18n({
locale: 'pl',
fallbackLocale: 'en',
messages: {
pl: {
station: {
name: 'NOVA LAB Station',
status: 'Status: {status}',
crew: 'Crew: {count} people'
}
},
en: {
station: {
name: 'NOVA LAB Station',
status: 'Status: {status}',
crew: 'Crew: {count} members'
}
}
}
})
app.use(i18n)In a component:
<script setup>
import { useI18n } from 'vue-i18n'
const { t, locale } = useI18n()
</script>
<template>
<h1>{{ t('station.name') }}</h1>
<p>{{ t('station.status', { status: 'Operational' }) }}</p>
<button @click="locale = 'en'">EN</button>
<button @click="locale = 'pl'">PL</button>
</template>VeeValidate - Form Validation
VeeValidate is a plugin for form validation:
<script setup>
import { useForm, useField } from 'vee-validate'
import * as yup from 'yup'
const schema = yup.object({
missionName: yup.string().required('Mission name is required'),
crewCount: yup.number().min(1, 'Minimum 1 person').required()
})
const { handleSubmit } = useForm({ validationSchema: schema })
const { value: missionName, errorMessage: nameError } = useField('missionName')
const { value: crewCount, errorMessage: crewError } = useField('crewCount')
const onSubmit = handleSubmit(values => {
console.log('Mission:', values)
})
</script>
<template>
<form @submit="onSubmit">
<input v-model="missionName" placeholder="Mission name" />
<span v-if="nameError">{{ nameError }}</span>
<input v-model="crewCount" type="number" placeholder="Crew" />
<span v-if="crewError">{{ crewError }}</span>
<button type="submit">Start mission</button>
</form>
</template>vue-toastification - Notifications
vue-toastification is a ready-made notification system:
import Toast from 'vue-toastification'
import 'vue-toastification/dist/index.css'
app.use(Toast, {
position: 'top-right',
timeout: 3000,
closeOnClick: true
})Creating a Plugin with provide/inject
The provide/inject pattern is the best way to share data from a plugin:
import { ref, readonly } from 'vue'
const NotificationPlugin = {
install(app, options = {}) {
const notifications = ref([])
let nextId = 0
function notify(message, type = 'info') {
const id = nextId++
notifications.value.push({ id, message, type })
setTimeout(() => {
notifications.value = notifications.value.filter(n => n.id !== id)
}, options.duration || 4000)
}
app.provide('notifications', {
list: readonly(notifications),
notify,
success: (msg) => notify(msg, 'success'),
error: (msg) => notify(msg, 'error'),
warning: (msg) => notify(msg, 'warning')
})
}
}The key element - readonly() prevents accidental modification of the notification list from outside the plugin.
Plugin Options Pattern
A good plugin accepts configuration with sensible default values:
const ThemePlugin = {
install(app, options = {}) {
const defaults = {
primaryColor: '#00ff88',
darkMode: true,
fontFamily: 'monospace',
borderRadius: '8px'
}
const theme = ref({ ...defaults, ...options })
function setTheme(newOptions) {
theme.value = { ...theme.value, ...newOptions }
}
app.provide('theme', {
current: readonly(theme),
setTheme
})
// Global directive using the theme
app.directive('themed', (el, binding) => {
const t = theme.value
el.style.color = t.primaryColor
el.style.fontFamily = t.fontFamily
if (binding.modifiers.bordered) {
el.style.border = '1px solid ' + t.primaryColor
el.style.borderRadius = t.borderRadius
}
})
}
}Installation:
app.use(ThemePlugin, {
primaryColor: '#00b4d8',
darkMode: true
})Auto-Install Pattern
Plugins intended for distribution (e.g., as an npm package) should support auto-install:
const MyPlugin = {
install(app, options) {
// plugin logic
}
}
// Auto-install when Vue is globally available (CDN)
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(MyPlugin)
}
export default MyPluginComposable vs Plugin - When to Use Which?
| Feature | Composable | Plugin |
|---|---|---|
| Registration | import in component | app.use() in main.js |
| Scope | Local (per component) | Global (entire application) |
| Global components | No | Yes (app.component) |
| Global directives | No | Yes (app.directive) |
| Provide/Inject | Possible | Natural |
| Dependency tree | Explicit imports | Implicit (inject) |
Rule of thumb: If the functionality is needed in many places and requires global configuration (e.g., theme, i18n, toast) - use a plugin. If it's logic specific to a few components - use a composable.
Code for this lesson: App.vue
<script setup>
import { inject, ref, readonly } from 'vue'
const theme = inject('theme')
const notifications = inject('notifications')
const { t, locale, setLocale } = inject('i18n')
function testNotification(type) {
if (type === 'success') {
notifications.success(t('notifications.saved'))
} else if (type === 'error') {
notifications.error(t('notifications.connectionError'))
} else {
notifications.notify(t('notifications.scanStarted'))
}
}
</script>
<template>
<div class="nova-lab">
<h1>Plugin Ecosystem and Integration</h1>
<section class="section">
<h2>Theme Plugin (provide/inject + readonly)</h2>
<div class="theme-info">
<p>Primary: <span :style="{ color: theme.current.value.primaryColor }">{{ theme.current.value.primaryColor }}</span></p>
<p>Dark Mode: {{ theme.current.value.darkMode ? 'YES' : 'NO' }}</p>
<p>Font: {{ theme.current.value.fontFamily }}</p>
</div>
<div class="btn-row">
<button @click="theme.setTheme({ primaryColor: '#00ff88' })" class="btn">Green</button>
<button @click="theme.setTheme({ primaryColor: '#00b4d8' })" class="btn cyan">Cyan</button>
<button @click="theme.setTheme({ primaryColor: '#e74c3c' })" class="btn red">Red</button>
</div>
</section>
<section class="section">
<h2>i18n Plugin</h2>
<p>{{ t('station.name') }}</p>
<p>{{ t('station.status') }}</p>
<div class="btn-row">
<button @click="setLocale('pl')" :class="['btn', { active: locale.value === 'pl' }]">PL</button>
<button @click="setLocale('en')" :class="['btn', { active: locale.value === 'en' }]">EN</button>
</div>
</section>
<section class="section">
<h2>Notification Plugin</h2>
<div class="btn-row">
<button @click="testNotification('success')" class="btn success">Success</button>
<button @click="testNotification('error')" class="btn error">Error</button>
<button @click="testNotification('info')" class="btn info">Info</button>
</div>
</section>
<div class="notif-container">
<div
v-for="n in notifications.list.value"
:key="n.id"
:class="['notif', n.type]"
>{{ n.message }}</div>
</div>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #c0c0c0;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 20px; }
h2 { color: #00b4d8; font-size: 16px; margin-bottom: 10px; }
.section {
background: rgba(0,180,216,0.05);
border: 1px solid rgba(0,180,216,0.2);
padding: 15px;
border-radius: 8px;
margin-bottom: 15px;
}
.theme-info { margin-bottom: 10px; }
.theme-info p { margin: 4px 0; font-size: 13px; }
.btn-row { display: flex; gap: 8px; }
.btn {
background: rgba(0,255,136,0.15);
color: #00ff88;
border: 1px solid #00ff88;
padding: 6px 14px;
border-radius: 6px;
cursor: pointer;
font-family: monospace;
}
.btn.active { background: #00ff88; color: #0a0e27; }
.btn.cyan { color: #00b4d8; border-color: #00b4d8; }
.btn.red { color: #e74c3c; border-color: #e74c3c; }
.btn.success { color: #2ecc71; border-color: #2ecc71; }
.btn.error { color: #e74c3c; border-color: #e74c3c; }
.btn.info { color: #00b4d8; border-color: #00b4d8; }
.notif-container {
position: fixed; top: 20px; right: 20px;
display: flex; flex-direction: column; gap: 8px; z-index: 1000;
}
.notif {
padding: 10px 18px; border-radius: 8px;
font-family: monospace; font-size: 13px;
animation: slideIn 0.3s ease;
}
.notif.success { background: rgba(46,204,113,0.9); color: white; }
.notif.error { background: rgba(231,76,60,0.9); color: white; }
.notif.info { background: rgba(0,180,216,0.9); color: white; }
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which of the following is the official Vue plugin for internationalization (i18n)?
2. Why use readonly() when exposing data from a plugin?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Implement a ThemePlugin with default options, a setTheme method, and a global v-themed directive.
- Horizontal ordering
Arrange the correct way to pass a readonly ref through provide:
- Click in order
Arrange the correct installation of ThemePlugin with options:
- Vertical ordering
Arrange the steps for implementing the Plugin Options Pattern:
- Horizontal ordering
Arrange the correct merging of default options with provided ones:
- Vertical ordering
Arrange the steps of the plugin auto-install pattern:
- Click in order
Arrange the correct registration of a global directive in a plugin:
- Code editor
Implement a notification plugin with a reactive array, setTimeout for auto-remove, and provide/inject.
- Vertical ordering
Arrange the layers of a Vue application architecture with plugins from lowest to highest:
- Horizontal ordering
Arrange the usage of the v-themed directive with the bordered modifier:
- Click in order
Arrange the correct registration of a global component in a plugin:
Tasks after this lesson
- QuestionPopular Vue plugins
- Questionreadonly() in plugins
- Coding taskPractice: Theme Plugin with Options
- Put in orderPassing a readonly ref from a plugin
- QuestionComposable vs Plugin
- Click in orderInstalling a plugin with options
- Put in orderPlugin Options Pattern
- Put in orderMerging default options
- Put in orderAuto-install pattern
- Click in orderGlobal directive in a plugin
- QuestionVeeValidate
- Coding taskPractice: Notification Plugin
- Put in orderApplication architecture with plugins
- Put in orderDirective with modifier in a plugin
- Click in orderRegistering a component in a plugin