Skip to content

Instantly share code, notes, and snippets.

@syuji-higa
Created December 18, 2019 04:44
Show Gist options
  • Save syuji-higa/d8ba8532d3c28daec762c55c4a00101e to your computer and use it in GitHub Desktop.
Save syuji-higa/d8ba8532d3c28daec762c55c4a00101e to your computer and use it in GitHub Desktop.
Nuxt.js - Navigation Guard
<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