Detailed explanation of vue page state persistence

Detailed explanation of vue page state persistence

Requirement : left tree and right table. After selecting a node in the organization tree, enter the details page and keep it selected when returning. Other query conditions also need to maintain state.

Idea : Use vuex combined with localStorage to cache data. When a tree node or query button is clicked, the data is saved in vuex and also in localStorage. When the page rolls back, the data in localStorage is put into vuex. In the page code, it is determined whether there is data stored in vuex. If there is, it is used directly. If not, the initial data is used by default. When the page navigation route jumps, the cached page data is cleared.

Code:

When a tree node is clicked, the id is saved in vuex. Triggering actions through dispatch

this.$store.dispatch('SetDeviceFaultId', data.id)

In vuex's actions, trigger the methods in mutations by committing

RemoveDeviceFaultId({ commit }) { //commit('REMOVE_DEVICEFAULTID') will be used later to clear data;
},
SetDeviceManageId({ commit }, id) {  
    commit('SET_DEVICEMANAGEID', id);
},

mutations : modify data

REMOVE_DEVICEFAULTID: (state) => {
      state.devicFaultId = null     
      Storage.remove('devicFaultId');
},
SET_DEVICEMANAGEID: (state, deviceManageId) => {
      state.deviceManageId = deviceManageId
      Storage.set('deviceManageId', deviceManageId);
}

state: Get cached data from localStorage when the page is first created

devicFaultId: Storage.get('devicFaultId'),

Enter a detail page and return

this.params.organizeId = this.$store.state.tree.devicFaultId ? this.$store.state.tree.devicFaultId : res.data[0].id;

When clicking on other routes, remember to clear the cached data

this.$store.dispatch('RemoveDeviceFaultId')

Replenish:

storage.js code

After introducing it in vuex, you can use it through Storage.set

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • How to implement data persistence using the vuex third-party package
  • Ideas and codes for implementing Vuex data persistence
  • Vuex implements data state persistence
  • Vuex persistence plug-in (vuex-persistedstate) solves the problem of refresh data disappearance
  • Implementation code of persistent storage data in vue project

<<:  Implementation of services in docker accessing host services

>>:  Summary of block-level elements, inline elements, and variable elements

Recommend

Javascript closure usage scenario principle detailed

Table of contents 1. Closure 2. Closure usage sce...

Example code of Vue3 encapsulated magnifying glass component

Table of contents Component Infrastructure Purpos...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

Detailed explanation of the execution order of JavaScript Alert function

Table of contents question analyze solve Replace ...

Gallery function implemented by native Js

Table of contents The first The second Native Js ...

Detailed explanation of Windows time server configuration method

Recently, I found that the company's server t...

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

Detailed explanation of the use of vue-resource interceptors

Preface Interceptor In some modern front-end fram...

JavaScript jigsaw puzzle game

This article example shares the specific code of ...

Implementation of Jenkins+Docker continuous integration

Table of contents 1. Introduction to Jenkins 2. I...

Text mode in IE! Introduction to the role of DOCTYPE

After solving the form auto-fill problem discussed...

What is Nginx load balancing and how to configure it

What is Load Balancing Load balancing is mainly a...

Detailed explanation of Linux netfilter/iptables knowledge points

Netfilter Netfilter is a packet processing module...