Extending Vue
Extend with Mixins
Mixins are a flexible way to distribute reusable functionalities for Vue components. You can write a mixin just like a normal Vue component option object:
1 | // mixin.js |
1 | // test.js |
Extend with Plugins
Plugins usually adds global-level functionality to Vue.
Writing a Plugin
There are typically several types of plugins you can write:
- Add one or more global methods. e.g. vue-element
- Add one or more global assets: directives/filters/transitions etc. e.g. vue-touch
- Add some Vue instance methods by attaching them to Vue.prototype. The convention here is Vue instance methods should be prefixed with
$
, so that they don’t conflict with user data and methods.
1 | exports.install = function (Vue, options) { |
Using a Plugin
Assuming using a CommonJS build system:1
2
3var vueTouch = require('vue-touch')
// use the plugin globally
Vue.use(vueTouch)
You can also pass in additional options to the plugin:
1 | Vue.use(require('my-plugin'), { |
Existing Plugins & Tools
- vue-router: The official router for Vue.js. Deeply integrated with Vue.js core to make building Single Page Applications a breeze.
- vue-resource: A plugin that provides services for making web requests and handle responses using a XMLHttpRequest or JSONP.
- vue-async-data: Async data loading plugin.
- vue-validator: A plugin for form validations.
- vue-devtools: A Chrome devtools extension for debugging Vue.js applications.
- vue-touch: Add touch-gesture directives using Hammer.js.
- vue-element: Register Custom Elements with Vue.js.
- List of User Contributed Tools
Next: Tips & Best Practices.
Caught a mistake or want to contribute to the documentation? Fork this site on Github!