Writing a Vue plugin is simple, and there is no need to learn more about Vue itself. The basic concept of a plugin is an object that needs to have an install function, which will be executed when called by the Vue.use() method. The install function will receive two arguments: Vue, and the options that will be used to instantiate the plugin.
Follow these instructions to write a plugin that adds two new functions to the Vue global prototype, $localStorage and $sessionStorage:
- In our project, we need to create a file inside the src/plugin folder named storageManipulator.js.
- In this file, we will create the plugin installation object – we'll add the default plugin options and the base prototype for the functions:
/* eslint no-param-reassign: 0 */
const defaultOption = {
useSaveFunction: true,
useRetrieveFunction: true,
onSave: value => JSON.stringify(value),
onRetrieve: value => JSON.parse(value),
};
export default {
install(Vue, option) {
const baseOptions...