平时我们在开发过程中,会遇到一种业务场景,就是避免编辑占用的问题,当一个用户编辑完成后,直接关闭浏览器,需要调用接口清除编辑占用状态。
` const handleBeforeUnload = async (event: any) => { if (localStorage.getItem('dashboardId')) { cleanEditUser.value = useSaltApi(cleanDashboardUser).fetchResource; const res = await cleanEditUser.value(localStorage.getItem('dashboardId'));
if (!res.success) {
ElMessage({
message: res.data,
type: 'warning',
showClose: true,
});
}
}
event.preventDefault();
event.returnValue = '';
};
window.addEventListener('beforeunload', event => {
handleBeforeUnload(event);
});
window.addEventListener('unload', event => {
handleBeforeUnload(event);
});`