Vue3弹出关闭页面提示
你要提示View.vue
<script>
import { onMounted, onBeforeUnmount } from "vue";
function beforeUnloadHandler(e: BeforeUnloadEvent) {
e.returnValue = ""; // 不为 null 即可
}
onBeforeUnmount(() => {
window.removeEventListener("beforeunload", beforeUnloadHandler);
});
onMounted(() => {
window.addEventListener("beforeunload", beforeUnloadHandler);
});
</script>
备注
Chrome60以后,如果用户打开浏览器后没有任何交互,直接关闭网页,则不会触发beforeunload事件。