在页面卸载之前发出请求

时间:2019-04-10 19:59:41

标签: firebase vue.js

我正在研究单页Vue应用程序。

在此应用程序中,我正在使用Firebase实时数据库来存储用户。

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

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

下面的代码不起作用...

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

1 个答案:

答案 0 :(得分:0)

您可以尝试:

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