Skip to content
the poor coder

vue localstore

Poor Man
Published
Reading time
2 min read
On this page

Vue Localstorage#

Vue Localstorage is a plugin which enables Vue.js developers to store key-value pairs in the browser's local storage in a more convenient way. With this plugin, you don't have to write the boilerplate code for getting and setting values in local storage. It also provides two-way binding between the properties of your Vue component and the values in local storage.

Installation#

You can install Vue Localstorage by running the following command:

Bash
npm install vue-localstorage

Usage#

First, you need to import the plugin in your main.js file:

JavaScript

import VueLocalStorage from 'vue-localstorage'

Vue.use(VueLocalStorage)

After importing the plugin, you can use it in your Vue components by defining a computed property with a getter and a setter. The computed property name should match the key under which you want to store the value in local storage.

HTML

<template>
  <div>
    <input v-model="name" />
    <p>Hello, {{ fullName }}!</p>
  </div>
</template>

<script>
export default {
  computed: {
    name: {
      get: function () {
        return this.$localStorage.get('name', '')
      },
      set: function (value) {
        this.$localStorage.set('name', value)
      }
    },
    fullName: function () {
      return this.name + ' Doe'
    }
  }
}
</script>

In this example, we have defined a computed property called "name" with a getter and a setter. The getter reads the value of "name" from local storage by calling the "$localStorage.get()" method. The setter saves the value of "name" to local storage by calling the "$localStorage.set()" method.

We also have another computed property called "fullName" which depends on the value of "name". Whenever the value of "name" changes, the value of "fullName" is automatically updated as well.

Alternative Usage#

If you prefer not to use computed properties, you can also access local storage directly in your methods or lifecycle hooks:

JavaScript

export default {
  mounted: function () {
    this.$localStorage.set('key', 'value')
    var value = this.$localStorage.get('key')
    console.log(value) // "value"
  }
}

In this example, we are setting a key-value pair in local storage in the "mounted" lifecycle hook. We then retrieve the value by calling the "$localStorage.get()" method and log it to the console.

Conclusion#

Vue Localstorage is a convenient way for Vue.js developers to store and retrieve key-value pairs in local storage. It helps reduce boilerplate code and provides two-way binding between Vue components and local storage values.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC