How to store object in localStorage in VueJS?

Hi Friends 👋,

Welcome To SortoutCode! ❤️

To store the object in localStorage in VueJS. We are going to use the localStorage.setItem() to store data with the JSON.stringify() method which converts an object into a string in localStorage.

Today I am going to show you How you store objects in localStorage in VueJS?

Table of contains

  • Setup the Vue.js
  • Create FirstComponent.vue and import it into App.js
  • What is localStorage?
  • Short solution
  • Short Example

Let’s start today’s tutorial How do I store object in localStorage in VueJS?

Setup the Vue.js

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation.

How to Install the VueJS project?

Create FirstComponent.vue and import it into App.js

Create the new component in the src/components/ components folder. The component name is FirstComponent.vue and imported into the App.js file:


  <div id="app">
    <FirstComponent />

import FirstComponent from "./components/FirstComponent.vue";
export default {
  name: "App",
  components: {

What is localStorage?

LocalStorage is the data storage type of web storage. This allows the Javascript sites and apps to store and access the data without any expiration date. The data will always and it will not expire even after the browser close.

Short solution

To store the object in localStorage, we are going to use the setItem() method to add the data in localStorage. setItem() method accepts the two parameters that are key and value. And using the JSON.stringify() method will convert the object into a string and store it in localstorage. Let’s see the code syntax:

let Obj = {

//store the name data in localStorage
localStorage.setItem('Obj',JSON.stringify(Obj));    //localStorage.setItem(key,value);

// get data from localStorage
let objt = JSON.parse(localStorage.getItem('Obj'));

console.log(objt);  // {id:1,sitename:'sortoutcode',website:''}

Short Example

Let’s understand with an example. First, we are going to store the object in localStorage.


    <h1>Data from localStorage</h1>
      <p v-if="objData">website : {{ }}</p>
      <button @click="addData">Add Data</button>
export default {
  name: "FirstComponent",
  data() {
    return {
      obj: {
        id: 1,
        sitename: "sortoutcode",
        website: "",
      objData: null,
  methods: {
    addData() {
      localStorage.setItem("Obj", JSON.stringify(this.obj));
      this.objData = JSON.parse(localStorage.getItem("Obj"));

For now, let’s check the output.


store object in localStorage in vuejs

All the best 👍.

Follow me on Twitter