我有一个 Vue 组件,它在“脏”(例如未保存)时进行跟踪。如果用户有未保存的数据,我想在他们浏览当前表单之前警告用户。在典型的 Web 应用程序中,您可以使用onbeforeunload. 我试图在这样的安装中使用它:
mounted: function(){
window.onbeforeunload = function() {
return self.form_dirty ? "If you leave this page you will lose your unsaved changes." : null;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这在使用 Vue Router 时不起作用。它可以让您根据需要导航尽可能多的路由器链接。只要您尝试关闭窗口或导航到真实链接,它就会警告您。
有没有办法onbeforeunload在 Vue 应用程序中复制普通链接和路由器链接?