简体   繁体   English

在元素未聚焦之前未添加或删除 BeforeUnload 侦听器

[英]BeforeUnload listener not added or removed before element is unfocused

I am working on a generic app that lets the user type in a formular and save, yadda yadda.我正在开发一个通用应用程序,让用户输入公式并保存,yadda yadda。 I want to ensure that the user is warned whenever changes aren't saved, both through a prompt triggered by in-App routing and by the beforeunload event.我想确保在未保存更改时警告用户,通过应用内路由和 beforeunload 事件触发的提示。 Both are supposed to be active when there has been changes, but revert to inactive if there aren't any changes/the form is empty.两者都应该在发生更改时处于活动状态,但如果没有任何更改/表单为空,则恢复为非活动状态。 The first works flawlessly.第一个完美无缺。 The second one, the "beforeunload" event is also readded and removed, but only if the input element that triggers a change to the "isEmpty / isDifferent" state is unfocused first.第二个,“beforeunload”事件也被读取和删除,但前提是触发更改“isEmpty / isDifferent”state 的输入元素首先未聚焦。 This is annoying because it doesn't provide instant feedback when the user starts typing, because the user is allowed to leave the page unwarned by refreshing even if the field isn't empty, because he hasn't unfocused the input.这很烦人,因为当用户开始输入时它不会提供即时反馈,因为即使字段不为空,用户也可以通过刷新使页面不被警告,因为他没有使输入失去焦点。 Or the user isn't "allowed" to leave the page, even if the input field is empty, because he didn't unfocus it before trying refreshing the page.或者即使输入字段为空,用户也不会“被允许”离开页面,因为他在尝试刷新页面之前没有取消焦点。

Here is the relevant code:以下是相关代码:

handleBeforeUnload (e) {
      console.log('leaving')
      e.preventDefault()
    },
    leaving (preventDefault) {
      if (preventDefault) {
        window.addEventListener('beforeunload', this.handleBeforeUnload)
        console.log('should be added')
      } else {
        window.removeEventListener('beforeunload', this.handleBeforeUnload)
        console.log('should be removed')
      }
    }
  },
  components: {
    'form-input': Input,
    modal: Modal
  },
  watch: {
    isEmpty () {
      this.leaving(!this.isEmpty)
    }
  },

Right now I have only configured the component to work on new documents, which is why I am only using "isEmpty" as a state right now.现在我只配置了组件来处理新文档,这就是为什么我现在只使用“isEmpty”作为 state。 The chain works as intended up until the "handleBeforeUnload" method, which can only be triggered if the user refreshes or similar after the input has been unfocused.在“handleBeforeUnload”方法之前,该链按预期工作,该方法只能在用户在输入未聚焦后刷新或类似情况下触发。 I am speculating that this behavior might be a result of Vue's lifecycle hooks, particularly "beforeDestroy" but I am not sure.我推测这种行为可能是 Vue 的生命周期钩子的结果,特别是“beforeDestroy”,但我不确定。 What is causing this behavior?是什么导致了这种行为? Thanks in advance.提前致谢。

Solved : This has been confirmed as a side effect of me using the "change" handler on the input fields, aka what seems to be the intended behavior of the change handler.解决:这已被确认为我在输入字段上使用“更改”处理程序的副作用,也就是更改处理程序的预期行为。 The DOM isn't updated before a "blur" event and this was discovered when I tried to disable the submit button through validation which resulted in the same issue. DOM 在“模糊”事件之前没有更新,当我尝试通过验证禁用提交按钮时发现了这一点,这导致了同样的问题。 Exchanging the "change" event on the input elements for a "keyup" solved this issue.将输入元素上的“change”事件换成“keyup”解决了这个问题。

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

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