[英]Show popup when close tab in Vue Js
當頁面重新加載或在 Vue js 中離開我的網站時,我試圖顯示一個彈出式確認。 我剛剛在我的組件中實現了beforeRouteLeave
函數,但這僅適用於我轉到站點中的另一個頁面時,而不是在我外出或重新加載頁面時。 我剛剛嘗試了window.addEventListener
函數,但彈出窗口出現在我所有的網站中。 我只想在這個 Vue 組件的頁面中顯示這個確認消息
有一些例子:
這是在 Vue 組件中:
beforeRouteLeave (to, from, next) {
const answer = window.confirm('Confermation Message')
if (answer) {
next()
} else {
next(false)
}
},
這是在 Vue 組件中,但在export default
之外:
window.addEventListener('beforeunload', (event) => {
if (logic) {
event.returnValue = 'Sei sicuro di uscire? Le modifiche non salvate andranno perse'
}
})
我能怎么做?
你可以做
const confirmExit = () => window.confirm('Sei sicuro di uscire? Le modifiche non salvate andranno perse')
window.addEventListener('beforeunload', evt => {
if (!confirmExit()) {
evt.preventDefault();
evt.returnValue = true;
}
});
然后在您的beforeRouteLeave
方法中,您還可以檢查if (confirmExit())
而不是if (answer)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.