0Pricing
Vue Academy · Lesson

Plugin Architecture and app.use()

Plugin object { install(app, options) }, accessing app.component, app.directive, app.provide.

Plugin Architecture and app.use() is a free Vue Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Vue Plugin

A Vue plugin is reusable code that adds app-level functionality — global components, directives, injected services, or config. Examples: Vue Router, Pinia, i18n libraries.

You register a plugin once with app.use(plugin).

The install Contract

A plugin is either an object with an install(app, options) method, or a function used directly as install. Vue calls it with the app instance and any options you pass.

const myPlugin = {
  install(app, options) {
    // register things on the app here
    console.log('installing with', options)
  }
}

export default myPlugin

Registering Global Components

Inside install, use app.component(name, definition) to register a component available in every template without importing it.

import BaseButton from './BaseButton.vue'
import BaseIcon from './BaseIcon.vue'

const uiKit = {
  install(app) {
    app.component('BaseButton', BaseButton)
    app.component('BaseIcon', BaseIcon)
  }
}

Registering Global Directives

Use app.directive(name, definition) to register a custom directive globally, such as a v-focus or v-tooltip.

const focusPlugin = {
  install(app) {
    app.directive('focus', {
      mounted(el) {
        el.focus()
      }
    })
  }
}

// usage in any template: <input v-focus />

Providing Injectable Services

app.provide(key, value) exposes a value that any descendant component can read with inject(key). This is the modern way to share services (an API client, config) without globals.

const apiPlugin = {
  install(app, options) {
    const client = createClient(options.baseURL)
    app.provide('api', client)
  }
}

Consuming the Provided Service

Any component uses inject to read what the plugin provided. Prefer a typed injection key (a Symbol) for safety, but string keys work too.

<script setup>
import { inject } from 'vue'

const api = inject('api')
async function load() {
  const data = await api.get('/users')
}
</script>

Passing Options to a Plugin

The second argument to app.use is forwarded as the options parameter of install. Use it to configure behavior at registration time.

import { createApp } from 'vue'
import App from './App.vue'
import apiPlugin from './plugins/api'

const app = createApp(App)
app.use(apiPlugin, { baseURL: 'https://api.example.com' })
app.mount('#app')

Plugins Run Before mount

All app.use calls happen before app.mount. By mount time, every global component, directive, and provided value is registered, so the first render already has access to them.

const app = createApp(App)
app.use(router)
app.use(pinia)
app.use(uiKit)
// everything is registered, now render
app.mount('#app')

Idempotent Installation

Vue guards against installing the same plugin twice on one app — the second app.use of an identical plugin is ignored. Still, design install to be safe and avoid side effects outside the app instance.

app.use(myPlugin) // installs
app.use(myPlugin) // ignored, no double registration

A Complete Minimal Plugin

Putting it together: a plugin can register a component, a directive, and provide a service all in one install method.

import Banner from './Banner.vue'

export default {
  install(app, options = {}) {
    app.component('AppBanner', Banner)
    app.directive('focus', { mounted: (el) => el.focus() })
    app.provide('config', { theme: options.theme || 'light' })
  }
}

Plugin vs Composable

Use a plugin for app-wide setup that runs once (global components, providing a singleton service). Use a composable for reusable stateful logic consumed per component. Plugins often set up what composables later read via inject.

Quick Check

Test your understanding of plugin architecture.

Recap

You learned plugin architecture:

  • A plugin exposes install(app, options) (or is a function)
  • app.component / app.directive register globals
  • app.provide exposes injectable services read via inject
  • app.use(plugin, options) registers it before mount
  • Installation is idempotent — installing twice is ignored

Frequently asked questions

Is the “Plugin Architecture and app.use()” lesson free?

Yes — the full text of “Plugin Architecture and app.use()” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “Plugin Architecture and app.use()”?

Plugin object { install(app, options) }, accessing app.component, app.directive, app.provide. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Plugin Architecture and app.use()” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Plugin Architecture and app.use()
  2. Global Properties and provide/inject in Plugins
  3. Notification and Toast Plugin Example
  4. Publishing and Typing Vue Plugins
← Back to Vue Academy