[Vuejs]-How to perform an action before user reload page or exit my vuejs application

0👍

Found a solution for my problem. I was putting the onbeforeunload event in the wrong place. And, in my case, the onbeforeunload wasn’t the answer. To log out the user I just needed to call:

import { EventBus } from "@/services/eventBus"

window.onunload = function () {
    EventBus.$emit("onunload")
}

… in main.js. Then, I was able to listen to this event and call a function in the .vue file:

mounted() {
    EventBus.$on("onunload", this.logout);
},

Leave a comment