Vue.js course ยท Module 14: Directives & Plugins
Plugins - Basics
In this lesson3
In the NOVA LAB Command Center, each subsystem (navigation, communication, diagnostics) is a separate module. In Vue, such modules are Plugins - a way to add global functionality to an application.
What Is a Plugin?
A plugin is an object with an install() method or a plain function. It is invoked by app.use():
// Plugin as an object
const myPlugin = {
install(app, options) {
// extend the application here
}
}
// Plugin as a function
function myPlugin(app, options) {
// also valid
}
// Registration
const app = createApp(App)
app.use(myPlugin, { /* options */ })What Can a Plugin Do?
In the install() method, you have access to the app instance and can:
1. Register Global Components
const UIPlugin = {
install(app) {
app.component('NovaButton', {
template: `<button class="nova-btn"><slot /></button>`
})
app.component('NovaCard', {
template: `<div class="nova-card"><slot /></div>`
})
}
}2. Register Global Directives
const DirectivesPlugin = {
install(app) {
app.directive('focus', {
mounted(el) { el.focus() }
})
app.directive('highlight', (el) => {
el.style.backgroundColor = '#00ff88'
})
}
}3. Share Data via provide/inject
const ConfigPlugin = {
install(app, options) {
app.provide('config', {
apiUrl: options.apiUrl || 'https://api.novalab.mars',
theme: options.theme || 'dark',
version: '2.0.87'
})
}
}
// Usage
app.use(ConfigPlugin, {
apiUrl: 'https://api.novalab.mars/v2',
theme: 'nova-dark'
})In any component:
<script setup>
import { inject } from 'vue'
const config = inject('config')
</script>
<template>
<p>API: {{ config.apiUrl }}</p>
<p>Version: {{ config.version }}</p>
</template>4. Add Global Properties
const UtilsPlugin = {
install(app) {
app.config.globalProperties.$formatDate = (date) => {
return new Date(date).toLocaleDateString('en-US')
}
}
}Plugin Options
The second parameter of install() is the options passed through app.use():
const LoggerPlugin = {
install(app, options = {}) {
const level = options.level || 'info'
const prefix = options.prefix || '[NOVA]'
app.provide('logger', {
info: (msg) => level !== 'error' && console.log(prefix, msg),
warn: (msg) => level !== 'error' && console.warn(prefix, msg),
error: (msg) => console.error(prefix, msg)
})
}
}
app.use(LoggerPlugin, {
level: 'info',
prefix: '[NOVA LAB]'
})Code for this lesson: App.vue
<script setup>
import { inject } from 'vue'
const config = inject('config')
const logger = inject('logger')
</script>
<template>
<div class="nova-lab">
<h1>Plugins - Basics</h1>
<section class="section">
<h2>Config Plugin (provide/inject)</h2>
<div class="info-grid">
<div class="info-item">
<span class="label">API URL:</span>
<span class="value">{{ config.apiUrl }}</span>
</div>
<div class="info-item">
<span class="label">Theme:</span>
<span class="value">{{ config.theme }}</span>
</div>
<div class="info-item">
<span class="label">Version:</span>
<span class="value">{{ config.version }}</span>
</div>
<div class="info-item">
<span class="label">Station:</span>
<span class="value">{{ config.station }}</span>
</div>
</div>
</section>
<section class="section">
<h2>Logger Plugin</h2>
<div class="btn-row">
<button @click="logger.info('System operational')" class="btn">Log Info</button>
<button @click="logger.warn('Low oxygen level')" class="btn warn">Log Warn</button>
<button @click="logger.error('Engine failure!')" class="btn error">Log Error</button>
</div>
<p class="hint">Open the browser console (F12) to see the logs</p>
</section>
<section class="section">
<h2>Global directive from plugin</h2>
<input v-focus placeholder="Auto-focus from plugin" class="input" />
</section>
</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;
}
.info-grid { display: grid; gap: 8px; }
.info-item { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid rgba(0,180,216,0.1); }
.label { color: #888; }
.value { color: #00ff88; }
.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.warn { color: #f39c12; border-color: #f39c12; background: rgba(243,156,18,0.15); }
.btn.error { color: #e74c3c; border-color: #e74c3c; background: rgba(231,76,60,0.15); }
.input {
background: rgba(0,255,136,0.05);
border: 1px solid #00ff88;
color: #00ff88;
padding: 10px;
border-radius: 6px;
font-family: monospace;
width: 100%;
box-sizing: border-box;
}
.input:focus { outline: none; box-shadow: 0 0 8px rgba(0,255,136,0.3); }
.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. What method must a Vue plugin object have?
2. How do you install a plugin in a Vue application?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Horizontal ordering
Arrange the correct app.use call with a plugin and options:
- Code editor
Use app.provide in the install method and inject in the component.
- Vertical ordering
Arrange the steps for creating and using a Vue plugin in the correct order:
- Click in order
Arrange the correct signature of the plugin install method: