Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Vue Plugins Create Install Plugins: Tutorial, Examples, FAQs & Interview Tips

What are Vue Plugins?

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.

Creating and Using Vue Plugins
// 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 tl-container
        const tl-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')

Deep Dive: Plugins in Real Projects

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.

Common Mistakes to Avoid

  • Mixing too many responsibilities in one component instead of separating logic by concern.
  • Skipping meaningful naming for variables, emits, and component props.
  • Ignoring edge cases like empty data, loading states, and error handling.
  • Optimizing too early before measuring real bottlenecks in browser devtools.
  • Not creating small test scenarios to validate behavior after each change.

Mini Practice Checklist

  1. Build a small demo focused only on Plugins.
  2. Add one edge case (empty/loading/error) and handle it cleanly.
  3. Refactor repeated logic into a reusable function/composable.
  4. Add clear comments only where logic is non-obvious.
  5. Verify behavior with manual testing and Vue Devtools.
Key Takeaways
  • This chapter on Plugins focuses on practical Vue 3 patterns used in real projects.
  • Prefer the Composition API with script setup for cleaner and more scalable component logic.
  • Keep components focused and move reusable logic into composables when complexity grows.
  • Use Vue Devtools to inspect component state, props, emits, and performance during development.
  • Write small experiments for each concept before applying it in production code.
  • After finishing this chapter, continue to the next related topic in the Vue roadmap.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.