Preface In the form filter items in the project, after selecting, the data will change when the page is refreshed, and it will not be retained on the option you selected. Tip: The following is the main content of this article. The following cases can be used for reference 1. Reasons:Vuex is a global data state management mechanism. The data in the store is stored in the running memory. When the page is refreshed, the page will reload the Vue instance, and the data in the store will be reassigned to the initialized state. 2. Solution ideas:Use vuex with local storage to refresh the page without losing data 1. Local storage method:
Extension: Cookie application scenarios (1) Determine whether the user has logged in to the website so that the user can log in automatically (or remember the password) the next time. If we delete cookies, you must fill in the login information again every time you log in. 2. Implementation steps:Since vue is a single-page application, all operations are completed on one page, and this project is only used in the currently opened project, so it is more appropriate to use sessionStorage
//store/selectData.js const state = {//data stored in state dataList: { exchangeIdSum: null, } } const mutations = { setExchangeIdSum(state, data) { //Reassign the selected data and save it to sessionStorage state.dataList.exchangeIdSum = data sessionStorage.setItem('dataList',JSON.stringify(state.dataList)) } setDataList(state, data) { state.dataList = JSON.parse(JSON.stringify(data)) } } const actions = { resetDataList: ({commit}, list) => { setTimeout(() => { commit('setDataList', list) }, 2000); } } export default { state, mutations, actions, } //Operation methods in the form filtering page: { exchangeChange(val) {//Modify the data in state when the drop-down box is selected this.$store.commit('selectData/setExchangeIdSum', val) }, } //App.vue in create(){//As soon as the page is entered, determine whether there is data in sessionStorage sessionStorage.getItem('dataList')? this.$store.dispatch('selectData/resetDataList', JSON.parse(sessionStorage.getItem('dataList'))):{} } 3. Optimization: Because when modifying state data before, each mutation must modify sessionStorage, but if there is a lot of state data to be modified, sessionStorage will be modified every time, which is a bit troublesome. solve: This concludes this article about how to use vuex combined with session storage data to solve the problem of data loss when refreshing a page. For more relevant vuex session storage data 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:
|
<<: Use Nginx to build a streaming media server to realize live broadcast function
>>: A brief introduction to MySQL storage engine
This article shares the specific code of JavaScri...
Web front-end optimization best practices: conten...
Table of contents Preface 🍹Preparation 🍲vue3 usag...
#docker search #docker pull portainer 1. Download...
Table of contents background Importing virtual fi...
Solution Abandon the Linux virtual machine that c...
First time using docker to package and deploy ima...
Table of contents Port-related concepts: Relation...
The effect to be achieved In many cases, we will ...
It is very convenient to configure virtual host v...
Table of contents 1. About JavaScript 2. JavaScri...
Adding the attribute selected = "selected&quo...
Table of contents 1. Check the current status of ...
Step 1: Add Ubuntu source Switch to root su root ...
You can see that their visual effects are very bea...