VueJS & Laravel,当用户离开屏幕时执行 post 请求

VueJS & Laravel, do a post request when user leaves screen

请寻求帮助。

我需要检查输入是否已填写,然后如果用户将屏幕留在选项卡或后退按钮等处,则会发生 post 请求。

 sendSMS: function(e) {
        if(this.to) {
            this.$http.post('/demo/send', message)
        }
 }

当输入不为空并且用户离开站点区域屏幕时,我需要说些什么,即转到选项卡或后退按钮,然后执行 post 请求。因此不会按下按钮来提交,而是检查输入是否已填写以及用户是否离开屏幕。

在我使用的上述方法中,如何使用 VueJS 执行此操作?

运行 页面关闭时的 js 函数:Run JavaScript code on window close or page refresh?

您需要在 Vue 的就绪函数中添加事件侦听器:

....
data:function(){
    return {sentRequest:false}
},
ready:function(){
    window.onbeforeunload = this.leaving;
    window.onblur = this.leaving;
    window.onmouseout = this.leaving;

},
methods:{
    leaving:function(){
        if(!this.sentRequest){
             //do stuff
             this.sentRequest = true;
        }
        return null;
    }
}
....