In Vue applications, some states are usually required to be persisted so that the previous state can still be retained after the user closes the browser or refreshes the page. Common ways of persistence includeLocalStorage
andSessionStorage
. This article will explain how to use these two methods to achieve state persistence.
LocalStorage
LocalStorage is a persistence method introduced in HTML5. It can store data in the browser and retain it after the user closes the browser. In Vue, we can use LocalStorage to save state data.
//Storage data('key', 'value'); // Get data('key'); // Delete data('key');
The above are three common methods of LocalStorage that can be used to store, obtain and delete data. Here is a simple counter as an example to demonstrate how to use LocalStorage to save state data.
<template> <div> <div>{{ count }}</div> <button @click="increment">+</button> </div> </template> <script> export default { data() { return { count: 0 } }, mounted() { // Get the value of count from LocalStorage const count = ('count'); if (count) { = parseInt(count); } }, methods: { increment() { ++; //Storage the value of count in LocalStorage ('count', ); } } } </script>
In the example above, we used the mounted lifecycle hook to get the value of count from LocalStorage when component loads. If there is a value of count in LocalStorage, we assign it to count. In the increment method, every time the counter is increased by 1, we store the value of count into LocalStorage so that the previous state can be restored the next time the component is loaded.
SessionStorage
SessionStorage is also a persistence method introduced in HTML5. It can store data in the browser and will be cleared after the user closes the browser tab. In Vue, we can use SessionStorage to save state data.
//Storage data('key', 'value'); // Get data('key'); // Delete data('key');
The above are three common methods of SessionStorage, which can be used to store, retrieve and delete data. The following is a simple login page as an example to demonstrate how to use SessionStorage to save state data.
<template> <div> <div v-if="isLoggedIn">Welcome,{{ username }}!</div> <div v-else>Please log in</div> <label>username:</label> <input type="text" v-model="username"> <label>password:</label> <input type="password" v-model="password"> <button @click="login">Log in</button> </div> </template> <script> export default { data() { return { username: '', password: '' } }, computed: { isLoggedIn() { return ('isLoggedIn') === 'true'; } }, methods: { login() { // Simulated login verification if ( === 'admin' && === '123456') { ('isLoggedIn', true); ('username', ); } } } } </script>
In the above example, we used the computed calculation attribute to determine whether the user is already logged in. In the login method, we simulate a login verification and store isLoggedIn and username into SessionStorage. In the computed computed property, we use the getItem method to get the value of isLoggedIn and convert it to a boolean for conditional rendering in the template.
Summarize
In Vue applications, we often need to persist some states so that the previous state can be retained after the user closes the browser or refreshes the page. This article introduces two common ways of persistence: LocalStorage and SessionStorage, and demonstrates how to use them in Vue applications to save state data.
In summary, using LocalStorage can store data in the browser and can still be retained after the user closes the browser. Using SessionStorage can store data in the browser and will be cleared after the user closes the browser tab. In Vue applications, we can use localStorage and sessionStorage global variables to access them, and use setItem, getItem and removeItem methods to store, get, and delete data.
When we need to persist state in Vue applications, we can consider using LocalStorage or SessionStorage to save state data. However, it should be noted that both methods have certain storage capacity limits, generally around 5MB. If you need to save a large amount of data, you may need to consider other persistence methods, such as using a server-side database to store data.
This is the end of this article about how to perform state persistence in Vue. For more related content on Vue state persistence, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!