在首次访问网页时,可以将部分数据放到localStorage中。
再次访问时,可以优先从localStorage拿出数据来做展示,避免白屏问题,然后,再去拉远程数据覆盖上来。
vue项目可以通过现成的vuex插件来做数据持久化,比如:vuex-persistedstate。
vue项目、小程序(uniApp)都可以使用,使用示例:
import Vue from "vue";import Vuex from "vuex";import createPersistedState from "vuex-persistedstate";Vue.use(Vuex);const modules = {};const context = require.context("./modules", false, /\.js$/);context.keys().map(key => {// 获取读取到的文件名字并且截取const name = key.slice(2, -3);modules[name] = context(key).default;});export default new Vuex.Store({modules,plugins: [createPersistedState({paths: ['theme','auth.openid','route.history','route.collectedPlaces','nearby.history','ad'],// uniapp小程序项目,需要配置好对应的存取删apistorage: {getItem: (key) => uni.getStorageSync(key),setItem: (key, value) => uni.setStorageSync(key, value),removeItem: (key) => uni.removeStorageSync(key),},filter: (mutation) => {const arr = ["theme/updateTheme","auth/setOpenid","route/setHistory","route/setCollectedPlaces","nearby/setHistory",'ad/updateInterstitialAdTime'];return arr.includes(mutation.type);},}),],});
