Creating a Vue.js Plugin
This article was AI-drafted and has not been reviewed yet. Spotted a mistake? Tell us.
Learn how to create a Vue.js plugin to extend the functionality of Vue.js.
Introduction to Vue.js Plugins
In this tutorial, we will learn how to create a Vue.js plugin to extend the functionality of Vue.js. This will allow us to add new features to Vue.js without modifying the core library.
Step 1: Creating the Plugin
First, we need to create the plugin. We can do this by creating a new file called myPlugin.js in the src directory:
export default {
install(Vue) {
Vue.prototype.$myPlugin = function() {
console.log('My plugin is working!')
}
}
}
Step 2: Installing the Plugin
Next, we need to install the plugin in our Vue.js application. We can do this by modifying the main.js file:
import Vue from 'vue'
import App from './App.vue'
import myPlugin from './myPlugin'
Vue.use(myPlugin)
Vue.config.productionTip = false
new Vue({
render: h => h(App)
}).$mount('#app')
Step 3: Using the Plugin
Finally, we can use the plugin in our Vue.js components:
<template>
<div>
<button @click='$myPlugin()'>Click me!</button>
</div>
</template>
Now we can run our application using npm run serve and see our plugin in action.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Vue.js courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Vue.js books — check the publication year so the examples match current versions.
Search Amazon →