How to use cookies or localstorage for data persistence in Vue
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
.
Use of cookies
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
localstorage
the 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 useJSON.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.localStorage
object 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.
Related Articles
Configuring Apache Web Server on Ubuntu and Debian
Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM
-
This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
My understanding of webservice is this
Publish Date:2025/03/18 Views:150 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:62 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
How to redirect a website from HTTP to HTTPS
Publish Date:2025/03/16 Views:117 Category:NETWORK
-
HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi
How to avoid soft 404 in the website during SEO
Publish Date:2025/03/17 Views:136 Category:NETWORK
-
This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand
Performance differences between HTTP REST API and WebSocket REST API
Publish Date:2025/03/17 Views:161 Category:NETWORK
-
What is a REST API? REST or RESTful API design (Representational State Transfer) is an architectural style that uses existing protocols. There are six key constraints for REST API design:
Webpack packages ES6 and CommonJs mixed React
Publish Date:2025/03/02 Views:179 Category:React
-
This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:201 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during