@seregpie/vue-storage

Allows the components to save and load their data across the browser sessions.
1,403 50
Install
npm install @seregpie/vue-storage
Latest Version:1.1.3
Vue:2 · 3
License:MIT
Last Updated:Jun 14, 2021
Links: GitHub  ·  npm
Maintainer: seregpie

VueStorage

Allows the components to save and load their data across the browser sessions.

Works for Vue 2 & 3.

demo

Try it out!

dependencies

setup

npm

npm i @seregpie/vue-storage

npm i @vue/composition-api # if using Vue 2

import VueStorage, {
  localStorage,
  sessionStorage,
  stored,
} from '@seregpie/vue-storage';

browser

<!-- if using Vue 2 -->
<script src="https://unpkg.com/vue@2"></script>
<script src="https://unpkg.com/@vue/composition-api"></script>

<!-- if using Vue 3 -->
<script src="https://unpkg.com/vue@3"></script>

<script src="https://unpkg.com/vue-demi"></script>
<script src="https://unpkg.com/@seregpie/vue-storage"></script>

The plugin is globally available as VueStorage.

usage

Composition API

import {stored} from '@seregpie/vue-storage';

export default {
  props: {
    userId: Number,
    userName: String,
  },
  setup(props) {
    let displayedUserName = stored(
      () => `myAwesomeApp/users/${props.userId}/name`,
      {
        type: String,
        default: () => props.userName,
      },
    );
    return {
      displayedUserName,
    };
  },
};

Options API

Install the plugin.

import {createApp} from 'vue'
import VueStorage from '@seregpie/vue-storage';

let app = createApp({/*...*/});
app.use(VueStorage, {
  prefix: 'myAwesomeApp/',
});
app.mount('body');

Define the options.

export default {
  props: {
    userId: Number,
    userName: String,
  },
  stored: {
    displayedUserName: {
      key() {
        return `users/${this.userId}/name`;
      },
      type: String,
      default() {
        return this.userName;
      },
    },
  },
};

If the key option is omitted, the property name is used instead.

stored: {
  backgroundColor: {
    type: String,
    default: '#fff',
  },
},

plugin options

app.use(VueStorage, {
  optionName: 'stored',
  prefix: '',
})
argument description
optionName A string as the name of the component option that contains all the stored properties.
prefix A string as the prefix for each storage key.

API

stored

stored(key, {
  type: JSON,
  default: null,
  session: false,
})

Creates a reference to a stored item.

argument description
key A string as the key. Can also be a getter function or a ref.
type An object with the parse and stringify functions to manage how the data is stored. Use Boolean, Number or String for a predefined functionality.
default Anything as the default value that is returned if the key does not exist. Can also be a getter function or a ref.
session If true, the session storage is used instead of the local storage. Can also be a getter function or a ref.

Returns the created reference.


let key = 'myAwesomeApp/numbers';
let r = stored(key, {
  type: {
    parse: (string => string.split('|').map(Number)),
    stringify: (array => array.join('|')),
  },
  default: [],
});

r.value = [1, 2];
console.log(r.value); // => [1, 2]
console.log(localStorage.getItem(key)); // => '1|2'

r.value = null;
console.log(r.value); // => []
console.log(localStorage.getItem(key)); // => null

localStorage

The reactive local storage.

Uses the same API as window.localStorage.

let key = 'myAwesomeApp/backgroundColor';
let backgroundColor = localStorage.getItem(key);
localStorage.removeItem(key);

sessionStorage

The reactive session storage.

Uses the same API as window.sessionStorage.

Related Packages

svue

Svue is a small (~2kb) library bringing Vue-style reactive stores with data and...

1,071 8
vue-easysession

A manage easy session storage plugin for VueJS

745 7
vue-sessionstorage

A Simple Plugin to Deal with SessionStorage on Vue.js

31,837 8
vue-session

A simplistic session plugin for VueJS backed by SessionStorage

159,920 200
vuex-easy-access

Unified syntax with simple set() and get() store access + auto generate mutation...

11,725 50

Version History

Version Released Vue License
1.1.3 MIT
1.1.2 MIT
1.1.1 MIT
1.1.0 MIT
1.0.2 ^3.0.0 MIT
Pre-releases (4)
Version Released Vue License
1.0.0-alpha.4 ^2.6.12 MIT
1.0.0-alpha.3 MIT
1.0.0-alpha.2 MIT
1.0.0-alpha.1 MIT