Save and Load Data Across Browser Sessions – VueStorage

Save and Load Data Across Browser Sessions – VueStorage

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

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/[email protected]"></script>
<script src="https://unpkg.com/@vue/composition-api"></script>

<!-- if using Vue 3 -->
<script src="https://unpkg.com/[email protected]"></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(
      () => `myApp/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: 'myApp/',
});
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 = 'myApp/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

Uses the same API as window.localStorage.

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

sessionStorage

Uses the same API as window.sessionStorage.

Download Details:

Author: SeregPie

Demo: https://seregpie.github.io/VueStorage/

Source Code: https://github.com/SeregPie/VueStorage

vue vuejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

8 Popular Websites That Use The Vue.JS Framework

In this article, we are going to list out the most popular websites using Vue JS as their frontend framework. Vue JS is one of those elite progressive JavaScript frameworks that has huge demand in the web development industry. Many popular websites are developed using Vue in their frontend development because of its imperative features.

Vue Native is a framework to build cross platform native mobile apps using JavaScript

Vue Native is a framework to build cross platform native mobile apps using JavaScript. It is a wrapper around the APIs of React Native. So, with Vue Native, you can do everything that you can do with React Native. With Vue Native, you get

How to Make a Simple Vue Custom Select Component

In this article, you’ll learn how to build a Vue custom select component that can be easily be styled using your own CSS. In fact, it’s the same component that we use in production on Qvault, and you can see it in action on the playground.

Creating a Custom Tooltip Component in Vue

There are plenty of libraries out there that will have you up and running with a good tooltip solution in minutes. However, if you are like me, you are sick and tired of giant dependency trees that have the distinct possibility of breaking at any time.

Vue ShortKey plugin for Vue.js

Vue-ShortKey - The ultimate shortcut plugin to improve the UX .Vue-ShortKey - plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.