简体   繁体   English

在 Vue Js 中关闭选项卡时显示弹出窗口

[英]Show popup when close tab in Vue Js

I'm trying to show a popup confermation when page reload or go outside my website in Vue js.当页面重新加载或在 Vue js 中离开我的网站时,我试图显示一个弹出式确认。 I just have implemented the beforeRouteLeave function in my component, but this works only with when I go to another page in my site, not when I go outside or I reload the page.我刚刚在我的组件中实现了beforeRouteLeave函数,但这仅适用于我转到站点中的另一个页面时,而不是在我外出或重新加载页面时。 I just already try window.addEventListener function but the popup appears in all my website.我刚刚尝试了window.addEventListener函数,但弹出窗口出现在我所有的网站中。 I want to show up this confermation message only in the page of this Vue component我只想在这个 Vue 组件的页面中显示这个确认消息

There are some example:有一些例子:

This is in the Vue Component:这是在 Vue 组件中:

beforeRouteLeave (to, from, next) {
  const answer = window.confirm('Confermation Message')
  if (answer) {
    next()
  } else {
    next(false)
  }
},

This is in the Vue Component, but outside the export default :这是在 Vue 组件中,但在export default之外:

window.addEventListener('beforeunload', (event) => {
  if (logic) {
    event.returnValue = 'Sei sicuro di uscire? Le modifiche non salvate andranno perse'
  }
})

How can i do?我能怎么做?

You could do你可以做

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;
  }
});

Then in your beforeRouteLeave method you can also check if (confirmExit()) instead of if (answer) .然后在您的beforeRouteLeave方法中,您还可以检查if (confirmExit())而不是if (answer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM