Analysis on the problem of data loss caused by forced refresh of vuex

Analysis on the problem of data loss caused by forced refresh of vuex

vuex-persistedstate

  1. Core principle: store all vuex data in local storage, fetch data from cache when page refreshes, and put it in vuex
  2. Download: $ npm install vuex-persistedstate -S
  3. Introducing plugins into the store
import persistedState from 'vuex-persistedstate'
const store = new Vuex.Store({
  // ...
  plugins: [persistedState()]
})

vuex-persistedstate uses localStorage for storage by default. If you want to use sessionStorage, you can use the following configuration

import persistedState from "vuex-persistedstate"
const store = new Vuex.Store({
  // ...
  plugins: [persistedState ({
      storage: window.sessionStorage
  })]
})
  • If you want to use cookies, you can use the following configuration
  • Download: $ npm install js-cookie -S
import Cookies from 'js-cookie';
import persistedState from "vuex-persistedstate"
const store = new Vuex.Store({
  // ...
  plugins: [persistedState ({
      storage:
		getItem: key => Cookies.get(key),
		setItem: (key, value) => Cookies.set(key, value),
		removeItem: key => Cookies.remove(key)
	}
  })]
})

secure-ls

  • Encrypted storage
  • When we save user information in vuex, although it is much more convenient to use, in order to solve the problem of data loss when vuex refreshes the page, the vuex-persistedstate plug-in is used. vuex-persistedstate is not encrypted, and the user's information is exposed in the cache.
  • It is very unsafe, so you need to use secure-ls to encrypt storage.
  • Download: $ npm install secure-ls -S
import Vue from "vue";
import Vuex from "vuex";
import SecureLS from 'secure-ls';
import persistedState from "vuex-persistedstate";

const ls = new SecureLS({
	encodingType: "aes", // encryption method isCompression: false, // whether to enable data compression encryptionSecret: "old-beauty" // 
});

Vue.use(Vuex);

export default new Vuex.Store({
	...
	plugins: [persistedState({
		// key: "123123", // key to store in storage
		storage:
			getItem: key => ls.get(key),
			setItem: (key, value) => ls.set(key, value),
			removeItem: key => ls.remove(key)
		}
	})],
});

[Note] vuex-persist(不兼容ie) vuex-persistedstate

This is the end of this article about vuex forced data loss. For more relevant vuex data loss content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to lose data after vuex page refresh
  • Solution to refreshing data loss when vuex stores complex parameters (such as object arrays, etc.)
  • Solution to data loss caused by vuex page refresh
  • Solution to data loss after vuex refresh
  • Four solutions to the problem of data loss when refreshing vuex pages

<<:  mysql executes sql file and reports error Error: Unknown storage engine'InnoDB' solution

>>:  Customization Method of Linux Peripheral File System

Recommend

Solve the problem of Tomcat10 Catalina log garbled characters

Running environment, Idea2020 version, Tomcat10, ...

How to use Linux paste command

01. Command Overview The paste command will merge...

MySQL 5.7.30 Installation and Upgrade Issues Detailed Tutorial

wedge Because the MySQL version installed on the ...

Vertical and horizontal splitting of MySQL tables

Vertical Split Vertical splitting refers to the s...

How to monitor the running status of docker container shell script

Scenario The company project is deployed in Docke...

Web Design Experience: 5 Excellent Web Design Concepts Full Analysis (Pictures)

Unlike other types of design, web design has been ...

Use of Vue3 table component

Table of contents 1. Ant Design Vue 1. Official w...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

Alibaba Cloud Ubuntu 16.04 builds IPSec service

Introduction to IPSec IPSec (Internet Protocol Se...

Detailed Analysis of or, in, union and Index Optimization in MySQL

This article originated from the homework assignm...

Vue3.x uses mitt.js for component communication

Table of contents Quick Start How to use Core Pri...

Linux tac command implementation example

1. Command Introduction The tac (reverse order of...

Calendar effect based on jQuery

This article example shares the specific code of ...

Vue integrates PDF.js to implement PDF preview and add watermark steps

Table of contents Achieve results Available plugi...