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

Solution to using html2canvas to process Dom elements with Baidu map into images

Problem 1: Baidu Map uses tiled images (the map i...

A brief discussion on HTML table tags

Mainly discuss its structure and some important pr...

VMware virtual machine installation CentOS 8 (1905) system tutorial diagram

The world-famous virtual machine software VMware-...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...

HTML sub tag and sup tag

Today I will introduce two HTML tags that I don’t...

JavaScript setTimeout and setTimeinterval use cases explained

Both methods can be used to execute a piece of ja...

What is Software 404 and 404 Error and what is the difference between them

First of all, what is 404 and soft 404? 404: Simpl...

Docker port mapping and external inaccessibility issues

The Docker container provides services and listen...

A brief discussion on the implementation principle of Webpack4 plugins

Table of contents Preface know Practice makes per...

How to add indexes to MySQL

Here is a brief introduction to indexes: The purp...

Vue-cli framework implements timer application

Technical Background This application uses the vu...

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface Ordinary users define crontab scheduled t...

Windows Server 2008 R2 Multi-User Remote Desktop Connection Licensing

At work, we often need remote servers and often e...