手机端,创建视频会议的场景
用户从首页home,点击创建会议,跳转到创建会议页面create,其中选择与会人员需要跳转到另一个联系人的页面contacts,勾选选联系人,选中联系人后,在返回到create页面,原来在create页面录入的数据要保持。
keep-alive指令可以将keep-alive中的组件缓存起来,下次访问不开始新的生命周期
路由的配置
{
path: "/create",
name: "create",
meta: {
keepAlive: true
},
component: appointment
},
路由的跳转 home页面
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
Vuex 保存需要缓存的路由
/*
* @Descripttion:
* @version:
* @Author: jsong
* @Date: 2020-03-26 13:38:38
* @LastEditors: jsong
* @LastEditTime: 2020-03-26 21:00:20
*/
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
includeKeepAlive: ["realTime", "appointment"]
},
mutations: {
changeAlive(state, value) {
const set = new Set(state.includeKeepAlive);
if (value.option === "d") {
set.delete(value.name);
} else {
set.add(value.name);
}
state.includeKeepAlive = [...set];
}
}
});
创建会议页面 create
// 只有添加会诊人员后缓存, 其他不缓存
beforeRouteLeave(to, from, next) {
// if (to.name === "contacts") {
// // to.meta.keepAlive = true;
// this.contactsVue = to;
// this.routeNum--;
// if (!from.meta.keepAlive) {
// from.meta.keepAlive = true;
// }
// } else {
// if (this.contactsVue) {
// // this.contactsVue.meta.keepAlive = false;
// }
// from.meta.keepAlive = false;
// setTimeout(() => {
// from.meta.keepAlive = true;
// }, 100);
// // this.$destroy();
// }
// console.log(this.$store.state.includeKeepAlive);
if (to.name != "contacts") {
// this.$destroy();
console.log(this.$options.name);
this.$store.commit("changeAlive", {
name: this.$options.name,
option: "d"
});
setTimeout(() => {
this.$store.commit("changeAlive", {
name: this.$options.name,
option: "a"
});
}, 100);
} else {
this.routeNum--;
}
next();
},
beforeRouteLeave(to, from, next) {
if (to.name === "contacts") {
// to.meta.keepAlive = true;
this.contactsVue = to;
this.routeNum--;
if (!from.meta.keepAlive) {
from.meta.keepAlive = true;
}
} else {
if (this.contactsVue) {
// this.contactsVue.meta.keepAlive = false;
}
from.meta.keepAlive = false;
setTimeout(() => {
from.meta.keepAlive = true;
});
// this.$destroy();
}
}