3

vuex页面刷新数据丢失问题的四种解决方式

 1 year ago
source link: https://www.fly63.com/article/detial/12242
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

vuex是大家使用vue时大多数都会选择的,但是当页面刷新之后vuex数据会丢失,下面这篇文章主要给大家介绍了关于vuex页面刷新数据丢失问题的四种解决方式,需要的朋友可以参考下

为什么说刷新页面vuex的数据会丢失

刷新页面vuex的数据会丢失属于正常现象,因为JS的数据都是保存在浏览器的堆栈内存里面的,刷新浏览器页面,以前堆栈申请的内存被释放,这就是浏览器的运行机制,那么堆栈里的数据自然就清空了。

第一种方法用sessionStorage

将接口返回的数据保存在vuex的store里,也将这些信息也保存在sessionStorage里。注意的是vuex中的变量是响应式的,而sessionStorage不是,当你改变vuex中的状态,组件会检测到改变,而sessionStorage就不会了,页面要重新刷新才可以看到改变,所以应让vuex中的状态从sessionStorage中得到,这样组件就可以响应式的变化。

在store文件夹里面的js文件 示例如下

const state = {
  authInfo: JSON.parse(sessionStorage.getItem("COMPANY_AUTH_INFO")) || {}
}

const getters = {
  authInfo: state => state.authInfo,
}
const mutations = {
  SET_COMPANY_AUTH_INFO(state, data) {
    state.authInfo = data
    sessionStorage.setItem("COMPANY_AUTH_INFO", JSON.stringify(data))
  }
}

//actions 模块里无需使用 sessionStorage

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  //actions,
}

其实这里还可以用 localStorage,但是它没有期限;所以常用的还是sessionStorage,当浏览器关闭时会话结束。

第二种方法是 vuex-along

(1)安装 vuex-along

npm install vuex-along --save

(2)在store文件夹里的index.js中引入vuex-along并配置相关代码

import Vue from 'vue'
import Vuex from 'vuex'
import indexOne from "./modules/indexOne"
import VueXAlong from 'vuex-along'

Vue.use(Vuex)
const store=new Vuex.Store({
    strict: false,
    modules:{
        indexOne
    },
    
plugins: [VueXAlong({
        name: 'along',     //存放在localStroage或者sessionStroage 中的名字
        local: false,      //是否存放在local中  false 不存放 如果存放按照下面session的配置配
        session: { list: [], isFilter: true }   
        //如果值不为false 那么可以传递对象 其中 当isFilter设置为true时, list 数组中的值就会被过滤调,这些值不会存放在seesion或者local中
      })]

})

export default store;

第三种方法是 vuex-persistedstate

(1)安装 vuex-persistedstate

npm install --save vuex-persistedstate

 (2)在store文件夹里的index.js中引入vuex-persistedstate并配置相关代码

import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    user
  },

plugins: [createPersistedState({
    storage: window.sessionStorage,
    reducer(val) {
      return { // 只储存state中的user 
        user: val.base
      }
    }
  })]
})

export default store;

第四种方法是 vuex-persist

(1)安装 vuex-persist

npm install --save vuex-persist
or
yarn add vuex-persist

(2)在store文件夹里的index.js中引入vuex-persist并配置相关代码

import Vue from 'vue'
import Vuex from 'vuex'
import indexOne from "./modules/indexOne"
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex)

const vuexLocal = new VuexPersistence({
    storage: window.localStorage
})

const store = new Vuex.Store({
    strict: false,
    modules:{
        indexOne,
    },
    plugins: [vuexLocal.plugin]
     
  }) 

export default store;

其实解决此问题的方法有很多,基本上都是要借助于localStorage或者sessionStroage来存放。

日常开发中,遇到数据量大的时候,一般在页面刷新的时候再次请求远程数据,使之动态更新vuex数据。

来自:https://www.cnblogs.com/baoxinyu/archive/2022/11/25/16925890.html

链接: https://www.fly63.com/article/detial/12242


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK