lesson 181: extended route guards unsaved data
This commit is contained in:
parent
47c9eddbf3
commit
5955eb1e59
|
@ -1,5 +1,6 @@
|
||||||
<template>
|
<template>
|
||||||
<button @click="confirmInput">Confirm</button>
|
<button @click="confirmInput">Confirm</button>
|
||||||
|
<button @click="saveChanges">Save Changes</button>
|
||||||
<ul>
|
<ul>
|
||||||
<user-item
|
<user-item
|
||||||
v-for="user in users"
|
v-for="user in users"
|
||||||
|
@ -18,10 +19,27 @@ export default {
|
||||||
UserItem
|
UserItem
|
||||||
},
|
},
|
||||||
inject: ['users'],
|
inject: ['users'],
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
changesSaved: false
|
||||||
|
};
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
confirmInput() {
|
confirmInput() {
|
||||||
console.log('fo');
|
|
||||||
this.$router.push('/teams');
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Reference in New Issue