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?')
})

Explanation

In-component guards know the component's state. Closing the tab needs a separate beforeunload listener.

Further reading

Problem with a question

Vue · Middle · question 60

Test yourself

Take the Vue · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz