JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

How to use cookies or localstorage for data persistence in Vue

Author:JIYIK Last Updated:2025/03/01 Views:

In Vue, we usually need to persist data so that the data status can be retained after the user refreshes the page or closes the browser. Common implementation methods include using cookies and localstorage.

Cookies are a simple client-side storage method that can store small amounts of data on the client. In Vue, you can use the js-cookie library to easily operate cookies.

First, you need to install js-cookie:

$ npm install js-cookie --save

Then, in the component that needs to use cookies, introduce js-cookie:

import Cookies from 'js-cookie'

Next, you can use Cookies.set()the method to set the cookie:

Cookies.set('key', 'value', { expires: 7 })

The first parameter is the cookie name, the second parameter is the cookie value, and the third parameter is an optional configuration item, such as expiration time.

Use Cookies.get()the method to get the cookie value:

Cookies.get('key')

Cookies.remove()Cookies can be deleted using the method:

Cookies.remove('key')

Using localstorage

localstorage is a way to store larger data on the client side, which can store multiple key-value pairs. In Vue, you can use the window.localStorage object to conveniently operate localstorage.

Using localstorage is similar to using cookies. First, you need to use setItem()the method to set localstorage:

window.localStorage.setItem('key', 'value')

Use getItem()the method to get the localstorage value:

window.localStorage.getItem('key')

Use removeItem()the method to delete localstorage:

window.localStorage.removeItem('key')

It should be noted that localstoragethe stored value must be of string type. If you need to store non-string data such as objects or arrays, you need to use the JSON.stringify() method to convert it to a string type, and use JSON.parse()the method to convert it back to the original type.

For example, to store an object:

const obj = { name: 'Jack', age: 18 }
window.localStorage.setItem('key', JSON.stringify(obj))

Get the object:

const objStr = window.localStorage.getItem('key')
const obj = JSON.parse(objStr)

Summarize

Both cookies and localstorage are ways to persist data on the client side. Choose the appropriate method based on actual needs. For scenarios where you need to store smaller data, cookies are more convenient; for scenarios where you need to store larger data, localstorage is more appropriate. In Vue, you can use the js-cookie library and window.localStorageobject to easily operate cookies and localstorage.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

What is the use of the immediate property of watch in Vue?

Publish Date:2025/03/01 Views:65 Category:Vue

In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang

Setting up checkbox functionality in Vue

Publish Date:2025/03/01 Views:185 Category:Vue

In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.

What happens if a child component changes the data in props in Vue?

Publish Date:2025/03/01 Views:138 Category:Vue

In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t

How to refresh the page in Vue

Publish Date:2025/03/01 Views:85 Category:Vue

Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the

How to find all elements by class name in Vue

Publish Date:2025/03/01 Views:182 Category:Vue

Vue is a very powerful JavaScript framework that provides developers with a lot of convenient features and tools. One of them is finding all elements by class name. In this article, we will explore how to find all elements by class name in

How to get the initial state of a certain data in Vue?

Publish Date:2025/03/01 Views:146 Category:Vue

In Vue, sometimes we want to get the initial state of a data in data so that we can compare or reset it in subsequent operations. In this article, we will introduce how to get the initial state of a data in data and provide an example to il

Implementation of scheduling execution in Vue project

Publish Date:2025/03/01 Views:128 Category:Vue

In Vue projects, scheduling execution refers to assigning tasks to different components or instances to implement functions such as data processing, rendering, and interaction. This article will introduce the implementation method of schedul

Defining global components in Vue

Publish Date:2025/03/01 Views:99 Category:Vue

In Vue, global components are a very important concept that allows us to use the same components everywhere. In this article, we will discuss how to define global components in Vue and the relevant precautions.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial