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

Example code of vue icon selector

Source: http://www.ruoyi.vip/ import Vue from ...

JavaScript event delegation principle

Table of contents 1. What is event delegation? 2....

Command to view binlog file creation time in Linux

Table of contents background analyze method backg...

Example of how to implement value transfer between WeChat mini program pages

Passing values ​​between mini program pages Good ...

How to use type enhancement without typingscript

Preface Due to the weak typing of JS, loose writi...

Vue detailed explanation of mixins usage

Table of contents Preface 1. What are Mixins? 2. ...

Detailed example of MySQL subquery

Subquery Classification Classification by returne...

How to build a virtual machine with vagrant+virtualBox

1. Introduction Vagrant is a tool for building an...

Install Kafka in Linux

Table of contents 1.1 Java environment as a prere...

Use of vuex namespace

Table of contents Since Vuex uses a single state ...

JavaScript DOMContentLoaded event case study

DOMContentLoaded Event Literally, it fires after ...

Negative distance (empathy) - iterative process of mutual influence

Negative distance refers to empathy. Preface (rai...

Solve the problem of resetting the Mysql root user account password

Problem description: The following error message ...

js implements custom drop-down box

This article example shares the specific code of ...