1、app.js
<template>
<div id="app">
<keep-alive :include="$store.state.keepAliveList">
<router-view/>
</keep-alive>
</div>
</template>
2、store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
keepAliveList: ['segmentSetting'],//segmentSetting该页面可以写死的,根据自己的需求
},
mutations: {
updateAliveList(state, { name, status }) {
if (status) {
state.keepAliveList.push(name);
} else {
const index = state.keepAliveList.indexOf(name);
index >= 0 && state.keepAliveList.splice(index, 1);
}
}
},
actions: {
},
modules: {
}
});
3、主页面(不用缓存页面)缓存的页面路由 let keepAliveRoute = ['/newBuildBatch', '/newBuild'];
beforeRouteEnter(to, from, next) {
console.log("获取数据====》",from);
if(from.path !== '/'){//跟页面刷新
const store = from.matched[0].instances.default.$store;
console.log("beforeRouteEnter to, from======>",to, from);
let keepAliveRoute = ['/newBuildBatch', '/newBuild'];
//判断是否需要清除缓存
keepAliveRoute.indexOf(from.path)!==-1? store.commit('updateAliveList', { name: from.name, status: false }):null;
}
next();
},
beforeRouteLeave(to, from, next) {
console.log("beforeRouteLeave to, from====>",to, from);
let keepAliveRoute = ['/newBuildBatch', '/newBuild'];
//判断是否需要缓存
keepAliveRoute.indexOf(to.path)!==-1? this.$store.commit('updateAliveList', { name: to.name, status: true }): null;
next();
},