How to implement data persistence using the vuex third-party package

How to implement data persistence using the vuex third-party package

Purpose:

Allow the state data managed in vuex to be stored locally at the same time. You can avoid the need for your own storage.

  • During the development process, user information (name, avatar, token) needs to be stored in vuex and locally.
  • If other modules also need to be persisted, they are also stored locally

1) First: We need to install a vuex plug-in vuex-persistedstate to support vuex state persistence.

npm i vuex-persistedstate

2) Then: create a modules file in the src/store folder, and create user.js and cart.js under modules

src/store/modules/user.js

// User module export default {
  namespaced: true,
  state () {
    return {
      // User information profile: {
        id: '',
        avatar: '',
        nickname: '',
        account: '',
        mobile: '',
        token: ''
      }
    }
  },
  mutations:
    // Modify user information, payload is the user information object setUser (state, payload) {
      state.profile = payload
    }
  }
}

3) Next: Import the user module in src/store/index.js.

import { createStore } from 'vuex'

import user from './modules/user'

export default createStore({
  modules:
    user
  }
})

4) Finally: Use the vuex-persistedstate plugin for persistence

import { createStore } from 'vuex'
+import createPersistedstate from 'vuex-persistedstate'

import user from './modules/user'

export default createStore({
  modules:
    user
  },
+ plugins: [
+ createPersistedstate({
+ key: 'erabbit-client-pc-store',
+ paths: ['user']
+ })
+ ]
})

Notice:

  • ===> By default, it is stored in localStorage
  • ===> key is the key name for storing data
  • ===> paths are the data stored in the state. If it is specific data under the module, the module name needs to be added, such as user.token
  • ===> Changes in local storage data can only be seen after the state is modified and triggered.

Summarize:

  • Implement data persistence in vuex based on third-party packages
  • The condition that triggers persistence is that the state changes

This is the end of this article about how to use the vuex third-party package to implement data persistence. For more relevant vuex data persistence content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Two implementation solutions for vuex data persistence
  • Ideas and codes for implementing Vuex data persistence
  • VUEX data persistence, example of re-acquisition after refresh
  • Vuex implements data state persistence
  • How to implement a simple vuex persistence tool
  • Detailed explanation of vuex persistence in practical application of vue

<<:  How to choose the right MySQL datetime type to store your time

>>:  Summary of MySQL database usage specifications

Recommend

Implementation of modifying configuration files in Docker container

1. Enter the container docker run [option] image ...

ThingJS particle effects to achieve rain and snow effects with one click

Table of contents 1. Particle Effects 2. Load the...

Introduction to the steps of deploying redis in docker container

Table of contents 1 redis configuration file 2 Do...

Example of downloading files with vue+django

Table of contents 1. Overview 2. Django Project 3...

In-depth understanding of Vue's plug-in mechanism and installation details

Preface: When we use Vue, we often use and write ...

What are inline elements and block elements?

1. Inline elements only occupy the width of the co...

docker cp copy files and enter the container

Enter the running container # Enter the container...

Vue installation and use

Table of contents 1. Vue installation Method 1: C...

MySQL configuration SSL master-slave replication

MySQL5.6 How to create SSL files Official documen...

Writing methods that should be prohibited in native JS

Table of contents Block-level functions Directly ...

Tutorial on installing mysql5.7.17 via yum on redhat7

The RHEL/CentOS series of Linux operating systems...

How to use yum to configure lnmp environment in CentOS7.6 system

1. Installation version details Server: MariaDB S...

Implementation of crawler Scrapy image created by dockerfile based on alpine

1. Download the alpine image [root@DockerBrian ~]...

Timeline implementation method based on ccs3

In web projects we often use the timeline control...