keep-alive 动态缓存

// router.beforeEach((to, from, next) => {
// 	let toView = to.matched.find(c => c.name === to.name);
// 	let fromView = from.matched.find(c => c.name === from.name);
// 	keepalive.keepaliveRules(toView, fromView);
// 	next();
// });
// 判断组件是否缓存
function keepaliveRules(toView, fromView) {
	if (!toView || !fromView) return;
	console.log(toView);
    // console.log(fromView.name);

	// 入驻管理 - 入驻协议管理
	if (toView.name === "enterprisesApplication" && fromView.name !== "enterprisesDetailes") {
		cleanKeepalive(toView.instances.default);
	}

	// 入驻管理 - 入驻协议管理
	if (toView.name === "immigrationAgreement" && fromView.name !== "immigrationAgreementEdit") {
		cleanKeepalive(toView.instances.default);
	}

	// 入驻管理 - 房租租赁协议管理
	if (toView.name === "leaseAgreement" && fromView.name !== "leaseAgreementedit") {
		cleanKeepalive(toView.instances.default);
	}

	// 入驻管理 - 入驻企业信息管理
	if (toView.name === "EnterprisesInformation" && fromView.name !== "EnterprisesInformationDetails") {
		cleanKeepalive(toView.instances.default);
	}

	// 工商代办管理 - 企业新设代办
	if (toView.name === "BusinessRegister" && fromView.name !== "BusinessRegisterDetaile") {
		cleanKeepalive(toView.instances.default);
	}

	// 工商代办管理 - 工商变更代办
	if (toView.name === "BusinessChange" && fromView.name !== "BusinessChangeDetaile") {
		cleanKeepalive(toView.instances.default);
	}

	// 装修管理 - 审核管理 验收管理
	let material = ["Application", "Acceptance"];
	if (material.includes(toView.name) && fromView.name !== "material") {
		cleanKeepalive(toView.instances.default);
	}

	// 政策兑现 - 政策兑现列表
	if (toView.name === "policyCash" && fromView.name !== "policyDetaile") {
		cleanKeepalive(toView.instances.default);
	}

	// 企业诉求管理
	if (toView.name === "AppealManagement" && fromView.name !== "AppealDetail") {
		cleanKeepalive(toView.instances.default);
	}

	// 企业推广管理
	if (toView.name === "PromotionManagement" && fromView.name !== "PromotionDetaile") {
		cleanKeepalive(toView.instances.default);
	}

	// 退驻管理 - 退驻审核管理
	if (toView.name === "FinishLeaseApplyList" && fromView.name !== "FinishLeaseApplyListDetails") {
		cleanKeepalive(toView.instances.default);
	}

	// 退驻管理 - 退驻协议管理
	if (toView.name === "FinishLeaseAgreementManage" && fromView.name !== "FinishLeaseAgreementManageEdit") {
		cleanKeepalive(toView.instances.default);
	}

	// 税务信息管理 - 税务知识管理
	if (toView.name === "taxKnows" && fromView.name !== "details") {
		cleanKeepalive(toView.instances.default);
	}

	// 第三方服务管理 - 第三方服务管理
	if (toView.name === "ThirdService" && fromView.name !== "thirdServiceDetails") {
		cleanKeepalive(toView.instances.default);
	}

	// 服务商调查 - 模板管理
	if (toView.name === "questionnaireTemplate" && fromView.name !== "QuestionnaireView") {
		cleanKeepalive(toView.instances.default);
	}

	// 房屋管理 - 园区管理
	if (toView.name === "estateManage" && fromView.name !== "estateDetailes") {
		cleanKeepalive(toView.instances.default);
	}

	// 房屋管理 - 楼宇管理
	if (toView.name === "buildingManage" && fromView.name !== "buildingDetailes") {
		cleanKeepalive(toView.instances.default);
	}

	// 房屋管理 - 房间管理
	if (toView.name === "floorManagement" && fromView.name !== "floorDetailes") {
		cleanKeepalive(toView.instances.default);
	}
}

// 清除不用缓存的组件
function cleanKeepalive(def) {
	if ( def.$vnode && def.$vnode.data.keepAlive) {
		if ( def.$vnode.parent && def.$vnode.parent.componentInstance && def.$vnode.parent.componentInstance.cache) {
			if(def.$vnode.componentOptions){
				var key = def.$vnode.key == null ? def.$vnode.componentOptions.Ctor.cid + (def.$vnode.componentOptions.tag ? `::${def.$vnode.componentOptions.tag}` : '') : def.$vnode.key;// 当前关闭的组件名
				// 找到虚拟dom
				let cache = def.$vnode.parent.componentInstance.cache; // 缓存的组件
				let keys = def.$vnode.parent.componentInstance.keys; // 缓存的组件名
				if (cache[key] != null) {
					cache[key].componentInstance.$destroy();
					let index = keys.indexOf(key);
					if (index > -1) {
						keys.splice(index, 1);
					}
				}
			}
			
		}
	}
}

const keepalive = {
	keepaliveRules: keepaliveRules
};
export default keepalive;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值