Vue.js course · Module 14: Directives & Plugins
Creating Useful Plugins
In this lesson3
In NOVA LAB, every station system is an independent module. Let's see how to create plugins that are truly useful: toast notifications and i18n (internationalization).
Toast Plugin - Notifications
A toast is a short notification displayed temporarily. The plugin will provide a show() method available throughout the entire application:
const ToastPlugin = {
install(app, options = {}) {
const duration = options.duration || 3000
const position = options.position || 'top-right'
const toasts = ref([])
let toastId = 0
const toast = {
show(message, type = 'info') {
const id = toastId++
toasts.value.push({ id, message, type })
setTimeout(() => {
toasts.value = toasts.value.filter(t => t.id !== id)
}, duration)
},
success(msg) { this.show(msg, 'success') },
error(msg) { this.show(msg, 'error') },
warning(msg) { this.show(msg, 'warning') },
list: toasts
}
app.provide('toast', toast)
}
}Usage in a component:
<script setup>
import { inject } from 'vue'
const toast = inject('toast')
function onSave() {
toast.success('Data saved!')
}
function onError() {
toast.error('Satellite connection error!')
}
</script>i18n Plugin - Multilingual Support
A plugin for translating texts into different languages:
const I18nPlugin = {
install(app, options) {
const locale = ref(options.defaultLocale || 'pl')
const messages = options.messages || {}
function t(key) {
const keys = key.split('.')
let result = messages[locale.value]
for (const k of keys) {
result = result?.[k]
}
return result || key
}
function setLocale(lang) {
if (messages[lang]) {
locale.value = lang
}
}
app.provide('i18n', {
t,
locale,
setLocale,
availableLocales: Object.keys(messages)
})
app.directive('t', (el, binding) => {
el.textContent = t(binding.value)
})
}
}Usage:
app.use(I18nPlugin, {
defaultLocale: 'pl',
messages: {
pl: {
nav: { home: 'Strona główna', settings: 'Ustawienia' },
status: { online: 'Połączony', offline: 'Rozłączony' }
},
en: {
nav: { home: 'Home', settings: 'Settings' },
status: { online: 'Connected', offline: 'Disconnected' }
}
}
})In a component:
<script setup>
import { inject } from 'vue'
const { t, setLocale, locale } = inject('i18n')
</script>
<template>
<nav>
<a>{{ t('nav.home') }}</a>
<a>{{ t('nav.settings') }}</a>
</nav>
<p>Status: {{ t('status.online') }}</p>
<button @click="setLocale('en')">EN</button>
<button @click="setLocale('pl')">PL</button>
</template>Good Plugin Patterns
- Provide/Inject - prefer over globalProperties
- Options with default values - the plugin must work without configuration
- Typing - export interfaces for TypeScript
- Cleanup - if the plugin modifies the DOM, ensure proper cleanup
- Documentation - describe available options and methods
Code for this lesson: App.vue
<script setup>
import { inject, ref } from 'vue'
const toast = inject('toast')
const { t, locale, setLocale } = inject('i18n')
function showSuccess() {
toast.success(t('toast.saved'))
}
function showError() {
toast.error(t('toast.connectionError'))
}
function showWarning() {
toast.warning(t('toast.lowOxygen'))
}
</script>
<template>
<div class="nova-lab">
<h1>{{ t('title') }}</h1>
<section class="section">
<h2>Toast Plugin</h2>
<div class="btn-row">
<button @click="showSuccess" class="btn success">{{ t('toast.successBtn') }}</button>
<button @click="showError" class="btn error">{{ t('toast.errorBtn') }}</button>
<button @click="showWarning" class="btn warn">{{ t('toast.warnBtn') }}</button>
</div>
</section>
<section class="section">
<h2>i18n Plugin</h2>
<p>{{ t('status.label') }}: {{ t('status.online') }}</p>
<p>{{ t('crew.label') }}: {{ t('crew.count') }}</p>
<div class="btn-row" style="margin-top: 10px">
<button
@click="setLocale('pl')"
:class="['btn', { active: locale === 'pl' }]"
>PL</button>
<button
@click="setLocale('en')"
:class="['btn', { active: locale === 'en' }]"
>EN</button>
</div>
<p class="hint">Switch language and see how the texts change</p>
</section>
<!-- Toast container -->
<div class="toast-container">
<div
v-for="t in toast.list.value"
:key="t.id"
:class="['toast', t.type]"
>
{{ t.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;
}
.btn-row { display: flex; gap: 10px; }
.btn {
background: rgba(0,255,136,0.15);
color: #00ff88;
border: 1px solid #00ff88;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-family: monospace;
}
.btn.active { background: #00ff88; color: #0a0e27; }
.btn.success { color: #2ecc71; border-color: #2ecc71; }
.btn.error { color: #e74c3c; border-color: #e74c3c; }
.btn.warn { color: #f39c12; border-color: #f39c12; }
.toast-container {
position: fixed; top: 20px; right: 20px;
display: flex; flex-direction: column; gap: 8px;
z-index: 1000;
}
.toast {
padding: 12px 20px;
border-radius: 8px;
font-family: monospace;
font-size: 14px;
animation: slideIn 0.3s ease;
}
.toast.success { background: rgba(46,204,113,0.9); color: white; }
.toast.error { background: rgba(231,76,60,0.9); color: white; }
.toast.warning { background: rgba(243,156,18,0.9); color: white; }
.toast.info { background: rgba(0,180,216,0.9); color: white; }
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
.hint { color: #666; font-size: 12px; margin-top: 8px; }
</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. How do you implement toasts that automatically disappear after 3 seconds?
2. What does the function t('nav.home') do in an i18n plugin?
Hands-on tasks in the game
- Vertical ordering
Arrange the steps of how the toast.show() method works in the correct order:
- Code editor
Implement a ToastPlugin with a reactive array of toasts and show/success/error methods.
- Click in order
Arrange the correct syntax of app.provide in a plugin:
- Horizontal ordering
Arrange the correct inject call to retrieve the toast: