欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue關(guān)閉瀏覽器退出登錄的實(shí)現(xiàn)示例

 更新時(shí)間:2021年12月03日 15:00:38   作者:星滅233  
本文主要介紹了vue關(guān)閉瀏覽器退出登錄,一般都是根據(jù)根據(jù)beforeunload和unload這兩個(gè)事件執(zhí)行的。本文就詳細(xì)的介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下

??項(xiàng)目需要:也在網(wǎng)上找了不少類似的文章,不過(guò)用起來(lái)多少有點(diǎn)問(wèn)題,畢竟要適合自己的需求,我這里是vue3適用,理論上vue2也可以使用,我寫的方法是通用的。

??這些方法無(wú)非都是根據(jù)beforeunloadunload這兩個(gè)事件執(zhí)行的。
??下面我搜了下菜鳥教程和MDN對(duì)兩個(gè)事件的介紹,可自行琢磨。

1、beforeunload事件

1.1、菜鳥教程:

在這里插入圖片描述

1.2、MDN

在這里插入圖片描述

2、unload事件

2.1、菜鳥教程

在這里插入圖片描述

2.2、MDN

在這里插入圖片描述
??MDN:通常而言,我們推薦使用 window.addEventListener() 來(lái)監(jiān)聽 unload (en-US) 事件,而不是直接給 onunload 賦值。

下面貼我使用的源碼;

3、源碼部分

3.1、方法一:可寫于html頁(yè)面使用(直接使用)

      var _beforeUnload_time = 0, _gap_time = 0;
      window.onunload = function (){
          _gap_time = new Date().getTime() - _beforeUnload_time;
          if(_gap_time <= 10) {//瀏覽器關(guān)閉
              window.mgr.signoutRedirect();//這個(gè)mgr是我暴露在window的退出登錄方法
          }else{//瀏覽器刷新-chrome刷新
              console.log(document.domain);
              return confirm("確定要離開本系統(tǒng)么?");
          }
      };
      window.onbeforeunload = function (){
          _beforeUnload_time = new Date().getTime();
      };

3.2、方法二:可寫于組件如app.vue使用(監(jiān)聽事件)

  data() {
    return {
      gap_time: 0,
      beforeUnload_time: 0,
    };
  },
  methods: {
    // 關(guān)閉窗口之前執(zhí)行
    beforeunloadHandler() {
      this.beforeUnload_time = new Date().getTime();
    },
    unloadHandler() {
      this.gap_time = new Date().getTime() - this.beforeUnload_time;
      //判斷是窗口關(guān)閉還是刷新 毫秒數(shù)判斷 網(wǎng)上大部分寫的是5
      if (this.gap_time <= 10) {
        mgr.signoutRedirect(); // 退出登錄接口 這里應(yīng)當(dāng)換為個(gè)人的登出方法
      } else {
        console.log(document.domain);
        return confirm("確定要離開本系統(tǒng)么?");
      }
    },
  },
  unmounted() {//vue可換為destroyed()生命周期,不過(guò)這個(gè)也可以用
    // 移除監(jiān)聽
    window.removeEventListener("beforeunload", () => this.beforeunloadHandler());
    window.removeEventListener("unload", () => this.unloadHandler());
  },
  mounted() {
    // 監(jiān)聽瀏覽器關(guān)閉
    window.addEventListener("beforeunload", () => this.beforeunloadHandler());
    window.addEventListener("unload", () => this.unloadHandler());
  },

參考文章:
??vue關(guān)閉瀏覽器時(shí),觸發(fā)事件,執(zhí)行退出登錄接口
??vue 關(guān)閉瀏覽器清空token (區(qū)分刷新)

到此這篇關(guān)于vue關(guān)閉瀏覽器退出登錄的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue關(guān)閉瀏覽器退出登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論