Understanding plugins
Vue plugins are a way to add custom functionality to Vue.js globally. Classic examples of plugin candidates are translation/internationalization libraries (such as i18n-next
) and HTTP clients (such as the axios
, fetch
, and GraphQL
clients). The plugin initializer has access to the Vue
instance, so it can be a good way to wrap global directives and components and inject resources across the application.
A Vue plugin is an object that exposes an install
method. The install
function is called with an app
instance and options
:
const plugin = { Â Â install(app, options) {} }
Within the install
method, we can register directives and components and add global and instance properties and methods:
const plugin = { Â Â install(app, options) { Â Â Â Â app.directive('fade', { bind() {} }) Â Â Â Â app.component(/*Register component globally*/) Â Â Â Â app.provide(/*Provide a resource to be...