Implement Vue routing guards with a dirty form guard. If the user tries to navigate away with unsaved changes, show a native browser confirm dialog. If they cancel, abort navigation. This must work for both in-app router pushes and browser back/forward buttons.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Providing Vue 3 composition API guard with beforeunload
The final solution uses a Vue 3 composition API form component with an isDirty state, a beforeRouteLeave guard to intercept in-app route changes (including back/forward), and a beforeunload event listener to catch page reloads or tab closes; both show a native confirm dialog and abort navigation if canceled.
You need to intercept two different things:
Navigations that stay inside your SPA (router pushes, history.back/forward), which are all driven by vue-router. These can be cancelled by returning next(false) from a route guard.
Navigations that tear down the page (tab close, hard refresh, navigating to another origin). Those never hit your Vue app, the only way to get a prompt is via beforeunload.
On the form component you can keep a simple “dirty” flag and wire both hooks up, for example with the Composition API in Vue 3:
ts
<script setup lang="ts">
import {ref, onMounted, onBeforeUnmount} from 'vue';
import {onBeforeRouteLeave} from 'vue-router';
const isDirty = ref(false);
// …update `isDirty.value` to `true` whenever the form changes, and to `false` on save.
const handleBeforeUnload = (e: BeforeUnloadEvent) => {
if (!isDirty.value) return;
// This tells the browser to show its native “leave site?” dialog.
e.preventDefault();
e.returnValue = '';
};
onMounted(() => {
window.addEventListener('beforeunload', handleBeforeUnload);
});
onBeforeUnmount(() => {
window.removeEventListener('beforeunload', handleBeforeUnload);
});
// Guard in-app navigations (router links, router.push, back/forward)
onBeforeRouteLeave((_to, _from, next) => {
if (!isDirty.value) {
next();
return;
}
if (window.confirm('You have unsaved changes. Do you really want to leave?')) {
// Clean up beforeunload listener when allowing navigation
window.removeEventListener('beforeunload', handleBeforeUnload);
next();
} else {
next(false); // abort navigation
}
});
</script>
<template>
<!-- your form -->
<form @input="isDirty = true">
…
</form>
</template>
With the Options API in Vue 2/Vue Router 3 the equivalent would be:
js
export default {
data() {
return { isDirty: false };
},
mounted() {
window.addEventListener('beforeunload', this.handleBeforeUnload);
},
beforeDestroy() {
window.removeEventListener('beforeunload', this.handleBeforeUnload);
},
methods: {
handleBeforeUnload(e) {
if (!this.isDirty) return;
e.preventDefault();
e.returnValue = '';
}
},
beforeRouteLeave(to, from, next) {
if (!this.isDirty) {
next();
} else if (window.confirm('You have unsaved changes. Do you really want to leave?')) {
window.removeEventListener('beforeunload', this.handleBeforeUnload);
next();
} else {
next(false);
}
}
};
In both cases, back/forward buttons trigger beforeRouteLeave, and a hard unload triggers the beforeunload listener, showing the native confirm dialog and aborting navigation when cancelled.