一.设置动态keepalive
可以将要缓存的页面作为vuex全局变量储存

(图片来源网络,侵删)
const cacheViewsState = store.state.app.cachedViews
将cachedViews 存入vuex:
state: { cachedViews: ['listPage'] }, mutations:{ ADD_CACHED_VIEW: (state, view) => { if (state.cachedViews.includes(view)) return state.cachedViews.push(view) }, DEL_CACHED_VIEW: (state, view) => { const index = state.cachedViews.indexOf(view) index > -1 && state.cachedViews.splice(index, 1) } }, actions: { //添加缓存组件 addCachedView({ commit }, view) { commit('ADD_CACHED_VIEW', view) }, //删除缓存组件 delCachedView({ commit, state }, view) { return new Promise((resolve) => { commit('DEL_CACHED_VIEW', view) resolve([...state.cachedViews]) }) } }
二.页面初始化数据缓存处理

(图片来源网络,侵删)
export default { name: 'listPage' }
onActivated 注册一个回调函数,若组件实例是 "> 缓存树的一部分,当组件从 DOM 中被移除时调用。
这个钩子在服务器端渲染期间不会被调用。
onActivated(() => { getList() // 初始化列表 })