Bird
Raised Fist0
Vueframework~5 mins

Plugin installation and usage in Vue

Choose your learning style10 modes available
Introduction

Plugins add extra features to your Vue app easily. They help you reuse code and add tools without rewriting.

You want to add global features like a date picker or a UI library.
You need to share functions or data across many components.
You want to add third-party tools like routing or state management.
You want to add custom behavior to Vue app globally.
You want to keep your app code clean by using ready-made solutions.
Syntax
Vue
import { createApp } from 'vue'
import MyPlugin from 'my-plugin'

const app = createApp(App)
app.use(MyPlugin, { option1: true })
app.mount('#app')
Use app.use() to install a plugin before mounting the app.
You can pass options to customize the plugin behavior.
Examples
Installs a toast notification plugin with a 3-second timeout.
Vue
import { createApp } from 'vue'
import ToastPlugin from 'vue-toastification'
import 'vue-toastification/dist/index.css'

const app = createApp(App)
app.use(ToastPlugin, { timeout: 3000 })
app.mount('#app')
Installs a custom plugin without options.
Vue
import { createApp } from 'vue'
import MyPlugin from './plugins/my-plugin.js'

const app = createApp(App)
app.use(MyPlugin)
app.mount('#app')
Installs a router plugin to manage app pages.
Vue
import { createApp } from 'vue'
import Router from './router'

const app = createApp(App)
app.use(Router)
app.mount('#app')
Sample Program

This example shows a simple logger plugin that adds a $log method globally. When you click the button, it logs a message to the console if logging is enabled.

Vue
/* plugins/logger.js */
export default {
  install(app, options) {
    app.config.globalProperties.$log = (msg) => {
      if (options?.enabled) {
        console.log(`LOG: ${msg}`)
      }
    }
  }
}

/* main.js */
import { createApp } from 'vue'
import App from './App.vue'
import LoggerPlugin from './plugins/logger.js'

const app = createApp(App)
app.use(LoggerPlugin, { enabled: true })
app.mount('#app')

/* App.vue */
<template>
  <button @click="sayHello">Click me</button>
</template>

<script setup>
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
function sayHello() {
  proxy.$log('Hello from plugin!')
}
</script>
OutputSuccess
Important Notes

Always install plugins before mounting the app.

Plugins can add global methods, directives, or components.

Check plugin docs for required options or setup steps.

Summary

Plugins add reusable features to Vue apps easily.

Use app.use() to install plugins with optional settings.

Plugins can add global properties, components, or behaviors.

Practice

(1/5)
1. What is the main purpose of using a plugin in a Vue application?
easy
A. To add reusable features or functionality globally
B. To write CSS styles for components
C. To create new Vue components manually
D. To replace the Vue router

Solution

  1. Step 1: Understand what plugins do in Vue

    Plugins provide reusable features or functionality that can be used across the whole app.
  2. Step 2: Compare options

    Options B, C, and D describe tasks unrelated to plugins' main purpose.
  3. Final Answer:

    To add reusable features or functionality globally -> Option A
  4. Quick Check:

    Plugins add reusable features = A [OK]
Hint: Plugins add global features to Vue apps [OK]
Common Mistakes:
  • Thinking plugins are for styling
  • Confusing plugins with components
  • Assuming plugins replace routing
2. Which is the correct way to install a plugin named MyPlugin in a Vue 3 app?
easy
A. usePlugin(app, MyPlugin)
B. app.use(MyPlugin)
C. app.install(MyPlugin)
D. Vue.use(MyPlugin)

Solution

  1. Step 1: Recall Vue 3 plugin installation syntax

    In Vue 3, plugins are installed by calling app.use() with the plugin.
  2. Step 2: Check options

    Vue.use(MyPlugin) (Vue.use(MyPlugin)) is Vue 2 syntax. Options A and C use invalid methods. app.use(MyPlugin) is correct.
  3. Final Answer:

    app.use(MyPlugin) -> Option B
  4. Quick Check:

    Vue 3 uses app.use() = D [OK]
Hint: Use app.use() to install plugins in Vue 3 [OK]
Common Mistakes:
  • Using Vue.use() which is Vue 2 syntax
  • Calling app.install() which does not exist
  • Trying custom functions like usePlugin()
3. Given this plugin code and usage:
const MyPlugin = {
  install(app, options) {
    app.config.globalProperties.$greet = () => `Hello, ${options.name}!`;
  }
};

const app = Vue.createApp({});
app.use(MyPlugin, { name: 'Alice' });
app.mount('#app');

// In a component template: {{ $greet() }}

What will be displayed inside the component?
medium
A. Error: $greet is not a function
B. Hello, undefined!
C. Hello, Alice!
D. Hello, !

Solution

  1. Step 1: Understand plugin install method

    The plugin adds a global method $greet that returns 'Hello, ' plus the name from options.
  2. Step 2: Check how plugin is used

    The plugin is installed with options { name: 'Alice' }, so $greet returns 'Hello, Alice!'.
  3. Final Answer:

    Hello, Alice! -> Option C
  4. Quick Check:

    Plugin method uses options.name = Alice [OK]
Hint: Plugin options pass data used in global methods [OK]
Common Mistakes:
  • Ignoring options parameter
  • Assuming $greet is undefined
  • Forgetting to use app.use() with options
4. What is wrong with this plugin installation code?
const app = Vue.createApp({});
app.mount('#app');
app.use(MyPlugin);
medium
A. Plugin must be installed before mounting the app
B. app.use() requires a second argument
C. Vue.createApp() is missing parentheses
D. app.mount() should be called twice

Solution

  1. Step 1: Check plugin installation order

    Plugins must be installed before calling app.mount() to ensure plugin features are available.
  2. Step 2: Analyze given code

    The code mounts the app first, then installs the plugin, which is too late.
  3. Final Answer:

    Plugin must be installed before mounting the app -> Option A
  4. Quick Check:

    Install plugins before mount = B [OK]
Hint: Always call app.use() before app.mount() [OK]
Common Mistakes:
  • Installing plugins after mounting
  • Thinking app.use() always needs options
  • Confusing createApp syntax
5. You want to create a plugin that globally registers a component MyButton and adds a global method $log to log messages. Which is the correct way to write the plugin's install method?
hard
A. install(app) { app.registerComponent('MyButton', MyButton); app.global.$log = msg => console.log(msg); }
B. install() { Vue.component('MyButton', MyButton); Vue.prototype.$log = msg => console.log(msg); }
C. install(app) { app.addComponent('MyButton', MyButton); app.globalProperties.log = msg => console.log(msg); }
D. install(app) { app.component('MyButton', MyButton); app.config.globalProperties.$log = msg => console.log(msg); }

Solution

  1. Step 1: Recall correct Vue 3 plugin API

    Use app.component() to register components and app.config.globalProperties to add global methods.
  2. Step 2: Check each option

    install(app) { app.component('MyButton', MyButton); app.config.globalProperties.$log = msg => console.log(msg); } uses correct methods and property names. Options A, B, and D use outdated or incorrect APIs.
  3. Final Answer:

    install(app) { app.component('MyButton', MyButton); app.config.globalProperties.$log = msg => console.log(msg); } -> Option D
  4. Quick Check:

    Use app.component and app.config.globalProperties = C [OK]
Hint: Use app.component and app.config.globalProperties in install [OK]
Common Mistakes:
  • Using Vue 2 global API like Vue.prototype
  • Wrong method names like registerComponent
  • Missing $ prefix on globalProperties method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard