lesson 181: extended route guards unsaved data

This commit is contained in:
Andreas Zweili 2021-05-04 15:02:09 +02:00
parent 47c9eddbf3
commit 5955eb1e59
1 changed files with 19 additions and 1 deletions

View File

@ -1,5 +1,6 @@
<template>
<button @click="confirmInput">Confirm</button>
<button @click="saveChanges">Save Changes</button>
<ul>
<user-item
v-for="user in users"
@ -18,10 +19,27 @@ export default {
UserItem
},
inject: ['users'],
data() {
return {
changesSaved: false
};
},
methods: {
confirmInput() {
console.log('fo');
this.$router.push('/teams');
},
saveChanges() {
this.changesSaved = true;
}
},
beforeRouteLeave(to, from, next) {
if (this.changesSaved) {
next();
} else {
const userWantsToLeave = confirm(
'Changes might be lost, do you want to continue?'
);
next(userWantsToLeave);
}
}
};