From a8c204661b7db2b3dad3c4fbe2d3b7321fc11725 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Thu, 7 Nov 2019 12:17:36 +0800 Subject: [PATCH] fixed #52 --- src/views/post/PostEdit.vue | 21 ++++++++++++++++++++- src/views/sheet/SheetEdit.vue | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/views/post/PostEdit.vue b/src/views/post/PostEdit.vue index 4a3add21..7b105b60 100644 --- a/src/views/post/PostEdit.vue +++ b/src/views/post/PostEdit.vue @@ -120,6 +120,7 @@ export default { if (this.attachmentDrawerVisible) { this.attachmentDrawerVisible = false } + window.onbeforeunload = null }, beforeRouteLeave(to, from, next) { if (this.postSettingVisible) { @@ -128,7 +129,25 @@ export default { if (this.attachmentDrawerVisible) { this.attachmentDrawerVisible = false } - next() + if (this.postToStage.originalContent) { + const answer = window.confirm('当前文章数据未保存,确定要离开吗?') + if (answer) { + next() + } else { + next(false) + } + } else { + next() + } + }, + mounted() { + window.onbeforeunload = function(e) { + e = e || window.event + if (e) { + e.returnValue = '当前文章数据未保存,确定要离开吗?' + } + return '当前文章数据未保存,确定要离开吗?' + } }, computed: { ...mapGetters(['options']) diff --git a/src/views/sheet/SheetEdit.vue b/src/views/sheet/SheetEdit.vue index 3bad874d..b3b409fe 100644 --- a/src/views/sheet/SheetEdit.vue +++ b/src/views/sheet/SheetEdit.vue @@ -110,6 +110,7 @@ export default { if (this.attachmentDrawerVisible) { this.attachmentDrawerVisible = false } + window.onbeforeunload = null }, beforeRouteLeave(to, from, next) { if (this.sheetSettingVisible) { @@ -118,7 +119,25 @@ export default { if (this.attachmentDrawerVisible) { this.attachmentDrawerVisible = false } - next() + if (this.sheetToStage.originalContent) { + const answer = window.confirm('当前页面数据未保存,确定要离开吗?') + if (answer) { + next() + } else { + next(false) + } + } else { + next() + } + }, + mounted() { + window.onbeforeunload = function(e) { + e = e || window.event + if (e) { + e.returnValue = '当前页面数据未保存,确定要离开吗?' + } + return '当前页面数据未保存,确定要离开吗?' + } }, computed: { ...mapGetters(['options'])