Vue in Vue is best learned by connecting the rule to a small admin screen. Start with the smallest component or composable, observe the output, and then add one realistic constraint so the concept becomes practical.
The key habit for this lesson is to watch ref, prop, slot, or emitted event as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.
Plugins are self-contained code that adds app-level functionality to Vue. A plugin is an object with an install(app, options) method (or just a function). Plugins can add global components, directives, provide values, or extend the app instance.
Common use cases: i18n (internationalization), toast notifications, analytics, HTTP clients, UI component libraries.
// plugins/toast.js - Toast notification plugin
import { ref, createApp, h } from 'vue'
// Toast component
const ToastComponent = {
props: { message: String, type: String },
template: `
<div :class="['toast', 'toast-' + type]">
{{ message }}
</div>
`
}
// Plugin object
export const ToastPlugin = {
install(app, options = {}) {
const defaultOptions = {
duration: 3000,
position: 'top-right',
...options
}
// Create toast container
const container = document.createElement('div')
container.id = 'toast-container'
document.body.appendChild(container)
// Toast function
function showToast(message, type = 'info') {
const toastApp = createApp(ToastComponent, { message, type })
const el = document.createElement('div')
container.appendChild(el)
toastApp.mount(el)
setTimeout(() => {
toastApp.unmount()
el.remove()
}, defaultOptions.duration)
}
// Provide the toast function globally
app.provide('toast', {
success: (msg) => showToast(msg, 'success'),
error: (msg) => showToast(msg, 'error'),
info: (msg) => showToast(msg, 'info'),
warning: (msg) => showToast(msg, 'warning'),
})
// Also add as global property
app.config.globalProperties.$toast = {
success: (msg) => showToast(msg, 'success'),
error: (msg) => showToast(msg, 'error'),
}
}
}
// Usage in component:
// import { inject } from 'vue'
// const toast = inject('toast')
// toast.success('Saved successfully!')
// toast.error('Something went wrong')
// plugins/i18n.js - Simple internationalization plugin
import { ref, provide, inject } from 'vue'
const I18N_KEY = Symbol('i18n')
export const i18nPlugin = {
install(app, options = {}) {
const { locale = 'en', messages = {} } = options
const currentLocale = ref(locale)
function t(key, params = {}) {
const keys = key.split('.')
let value = messages[currentLocale.value]
for (const k of keys) {
value = value?.[k]
}
if (!value) return key // fallback to key
// Replace {param} placeholders
return value.replace(/\{(\w+)\}/g, (_, param) => params[param] ?? `{${param}}`)
}
function setLocale(newLocale) {
currentLocale.value = newLocale
}
// Provide globally
app.provide(I18N_KEY, { t, currentLocale, setLocale })
// Global property
app.config.globalProperties.$t = t
}
}
// Composable to use in components
export function useI18n() {
return inject(I18N_KEY)
}
// Usage in main.js:
// app.use(i18nPlugin, {
// locale: 'en',
// messages: {
// en: { greeting: 'Hello, {name}!', nav: { home: 'Home' } },
// es: { greeting: 'Hola, {name}!', nav: { home: 'Inicio' } }
// }
// })
// Usage in component:
// const { t, setLocale } = useI18n()
// t('greeting', { name: 'Alice' }) // 'Hello, Alice!'
// setLocale('es')
// main.js - registering plugins
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import { ToastPlugin } from './plugins/toast'
import { i18nPlugin } from './plugins/i18n'
// Import third-party plugins
import VueClickAway from 'vue3-click-away'
const app = createApp(App)
// Use plugins - order matters
app.use(createPinia())
app.use(router)
// Custom plugins with options
app.use(ToastPlugin, { duration: 4000, position: 'bottom-right' })
app.use(i18nPlugin, {
locale: 'en',
messages: {
en: {
greeting: 'Hello, {name}!',
nav: { home: 'Home', about: 'About' },
errors: { required: 'This field is required' }
},
fr: {
greeting: 'Bonjour, {name}!',
nav: { home: 'Accueil', about: 'A propos' },
errors: { required: 'Ce champ est obligatoire' }
}
}
})
// Third-party plugin
app.use(VueClickAway)
// Global component registration (often done in plugins)
import BaseButton from './components/BaseButton.vue'
import BaseInput from './components/BaseInput.vue'
app.component('BaseButton', BaseButton)
app.component('BaseInput', BaseInput)
// Global directive registration
import { vFocus } from './directives'
app.directive('focus', vFocus)
app.mount('#app')
Understanding Plugins is not just about syntax. In production applications, this topic directly affects maintainability, debugging speed, and team collaboration. Focus on readability, small reusable patterns, and predictable state flow when implementing Plugins.
A practical approach is to first implement the simplest working version, then refactor into reusable pieces (components/composables/stores) only when duplication appears. This helps keep your Vue codebase clean while avoiding over-engineering.
Use Vue when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real Vue task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.
A reliable practice flow is: create the smallest working component or composable, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with Vue Devtools and template warnings. If the result surprises you, reduce the code until the behavior is visible again.
The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why Vue is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.
Copying the syntax before understanding the behavior.
Write the expected behavior first, then make the example prove it.
Practicing only the perfect input.
Also test missing, repeated, empty, or boundary input before considering the lesson complete.
Looking only at the final output.
Trace ref, prop, slot, or emitted event through each important step.
Use it when the problem matches the behavior shown in the example and when the result can be verified through Vue Devtools and template warnings.
Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.
Trace ref, prop, slot, or emitted event, predict the result, run the example, and compare your prediction with the actual output.
Explore 500+ free tutorials across 20+ languages and frameworks.