首页 > 解决方案 > 在页面卸载之前提出请求

问题描述

我正在开发单页 vue 应用程序。

在这个应用程序中,我使用 firebase 实时数据库来存储用户。

我只想在页面卸载之前更新用户在 firebase 上的“isOnline”状态。

所以,我想发出http请求。可能吗 ?

下面的代码不能解决问题......

new Vue({
  created(){
   window.onbeforeunload = function() {
     apiservice.update(this.user);
     return null
    };
  },
}

标签: firebasevue.js

解决方案


你可以试试 :

new Vue({
  created () {
    window.addEventListener('beforeunload', this.updateUser)
  },
  methods: {
    updateUser () {
        console.log('updating user')
    }
  }
})

推荐阅读