Created
December 18, 2019 04:44
-
-
Save syuji-higa/d8ba8532d3c28daec762c55c4a00101e to your computer and use it in GitHub Desktop.
Nuxt.js - Navigation Guard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<div v-if="isLeavePageDialogOpened" class="page-leave-dialog"> | |
<p>Are you sure you want to change the page?</p> | |
<div> | |
<button type="button" @onClick="leavePageNext(false)">No</button> | |
<button type="button" @onClick="leavePageNext(true)">Yes</button> | |
</div> | |
</div> | |
</div> | |
</template> | |
<script> | |
export default { | |
beforeRouteLeave(to, from, next) { | |
// // simple | |
// const result = Window.confirm('Are you sure you want to change the page?') | |
// next(result) | |
// original dialog | |
this.openLeavePageDialog(next) | |
}, | |
data() { | |
return { | |
isLeavePageDialogOpened: false, | |
leavePageNext: () => {} | |
} | |
}, | |
methods: { | |
openLeavePageDialog(next) { | |
this.leavePageNext = (result) => { | |
next(result) | |
this.isLeavePageDialogOpened = false | |
} | |
this.isLeavePageDialogOpened = true | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment