Writing a Vue plugin is rather easy. You just need to use an install method in your plugin to accept Vue as the first argument and options as the second argument:
// plugin.js
export default {
install (Vue, options) {
// ...
}
}
Let's create a simple custom greeting plugin in different languages for a standard Vue app. The language can be configured through the options parameter; English will be used as the default language when no option is provided:
- Create a /plugins/ folder in the /src/ directory with a basic.js file in it with the following code:
// src/plugins/basic.js
export default {
install (Vue, options) {
if (options === undefined) {
options = {}
}
let { language } = options
let languages = {
'EN': 'Hello!',
'ES': 'Hola!'
}
if (language === undefined) {
language = 'EN'
}
Vue.prototype.$greet = (name) =...