Writing a very simple plugin in Vue.js - (Example)
Tags: webdev, javascript, vue, showdev
Published: 2018-07-17 09:42:52

---
title: Writing a very simple plugin in Vue.js - (Example)
published: true
description: Do you want global-level functionality to your Vue project? Create a plugin.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/jgbvo4nde0z98mlsusra.png
tags: webdev, javascript, vue, showdev
---
- What is a Vue Plugin?
- Writing and using a plugin

In this post, i will be showing you how you can write a basic plugin in Vue.
*You should have a prior experience with Vue and JavaScript.*

## What is a Vue Plugin?
Enables you to add global-level functionality to Vue.
It's a good way to keep your Vue components clear and small. You can also package and share your code with other developers. 
Specifically, plugin is nothing more than an object which expose an `install` method.

## Writing and using a plugin
Firstly, you should export the plugin object, so it can be used anywhere in your project. Then expose the `install` method which takes two parameters: `Vue` constructor and `options` object. The second parameter - `options` - is optional and customize your plugin.
**`plugin.js`**
```javascript
// plugin.js

// This exports the plugin object.
export default {
  // The install method will be called with the Vue constructor as
  // the first argument, along with possible options
  install (Vue, options) {
  }
}
```

But a question which arises is where do I provide these `options`?

OK. You can call `use()` method and pass `yourPlugin` and your `options` as arguments, since you have already imported your `plugin.js` into `main.js` file.

**`main.js`**
```javascript
// main.js

import Vue from 'vue'
import yourPlugin from './plugin.js'

// Without options
Vue.use(yourPlugin)
// With options
Vue.use(yourPlugin, {
    someOption: true
})

new Vue({
  el: '#app',
  render: h => h(App)
})
```
[Install method - Plugins](https://vuejs.org/v2/guide/plugins.html)
The `install` method contains one or more of the following: 
- Add some global methods or properties.
- Add one or more global assets: directives/filters/transitions etc.
- Add some component options by global [mixin](https://vuejs.org/v2/guide/mixins.html).

> Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component uses a mixin, all options in the mixin will be “mixed” into the component’s own options.

- Add some Vue instance methods by attaching them to Vue.prototype.

```javascript
// plugin.js
import Component from '/src/components/Component.vue'

// This exports the plugin object.
export default {
  // The install method will be called with the Vue constructor as         
  // the first argument, along with possible options
  install (Vue, options) {
     // Add or modify global methods or properties.
     Vue.yourMethod = (value) => value
     // Add a component or directive to your plugin, so it will be installed globally to your project.
     Vue.component('component', Component)
     // Add `Vue.mixin()` to inject options to all components.
     Vue.mixin({
         // Add component lifecycle hooks or properties.
         created() {
            console.log('Hello from created hook!')
         }
     })
     // Add Vue instance methods by attaching them to Vue.prototype.
     Vue.prototype.$myProperty = 'This is a Vue instance property.'
  }
}
``` 

#### Now, let's take a deep dive into our example's code

--- EXAMPLE ---

{% gist https://gist.github.com/nkoik/0f1beb1f757d3e0c79ad0b76c11e1a80 %}

### Distributing
So you have built your first plugin in Vue. It's time to distribute it to the community - if you want. 
- Upload the source files to [Github](https://github.com/) and then 
- Publish the package to [NPM](https://www.npmjs.com/).

*Until next time...Happy coding!*