What is vuexvuex: is a state manager developed specifically for vue.js, which uses centralized storage of all component states Five attributes: state, getters, mutations, actions, module Basic use: Create a new store.js file, finally introduce it in main.js, and mount it to the real column import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const state = {} const getters = {} const mutations = {} const actions = {} export default new Vuex.Store({ state, getters, mutations, actions }) state attribute: stores the state, such as the data you want to store Getters: Similar to shared properties, you can use this.$store.getters to get the data stored in the state mutations: The only way to change the state is by submitting mutations, this.$store.commit() Actions: One-step mutations, which can be distributed through dispatch to change the state Vuex data persistenceAs we all know, Vuex data is stored in memory, and these data will be lost if you refresh the web page. We hope that some data can still be retained after refreshing, which requires storing the data. Here is a record of using localStorage to persist the data in Vuex. Implementation ideas
Code Plugins: export default (options = {}) => { const storage = options.storage || (window && window.localStorage); const key = options.key || "vuex"; // Get the value of state const getState = (key, storage) => { const value = storage.getItem(key); try { return typeof value !== "undefined" ? JSON.parse(value) : undefined; } catch (err) { console.warn(err); } return undefined; }; // Set the value of state const setState = (key, state, storage) => storage.setItem(key, JSON.stringify(state)); return (store) => { // Get data during initialization. If any, replace the original vuex state const data = Object.assign(store.state, getState(key, storage)); if (data) { store.replaceState(data); } // Subscribe to store mutation. The handler is called after each mutation is completed, receiving the mutation and the state after the mutation as parameters store.subscribe((mutation, state) => { setState(key, state, storage); }); }; }; Calling method: import VuexPersist from "@/plugins/VuexPersist"; export default createStore({ plugins: [VuexPersist()], }); SummarizeThis is the end of this article about the implementation of Vuex 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:
|
<<: Linux common text processing commands and vim text editor
>>: Detailed explanation of the error problem of case when statement
Several typical values of innodb_flush_method f...
Table of contents Preface 1. GMT What is GMT Hist...
Three functions: 1. Automatic vertical centering o...
Table of contents Interpolation Expressions metho...
Does performance really matter? Performance is im...
In the latest version of Ubuntu, users no longer ...
Ubuntu 18.04 installs mysql 5.7 for your referenc...
Some projects have relatively simple business, bu...
Table of contents Common payment methods in proje...
In the horizontal direction, you can set the alig...
This article shares the shell script of mysql5.6....
Table of contents Preface Initialize the project ...
Perfect solution to the scalable column problem o...
Simple implementation of Mysql add, delete, modif...
Table of contents 1. Easy to read code 1. Unified...