Vue · Middle · Question 60 of 75
How do you warn the user before leaving a route with unsaved changes?
onBeforeRouteLeave(() => {
if (dirty.value) return confirm('Leave without saving?')
})- ✓Correct answer: Use
onBeforeRouteLeaveand returnfalseto cancel navigation - 2Use
onUnmounted - 3Use
router.beforeEachonly
Explanation
In-component guards know the component's state. Closing the tab needs a separate beforeunload listener.