退出登錄后清除keepalive緩存

最近項目中被一個keep-alive的問題困擾了很久,項目中有四個頁面使用了keep-alive,導(dǎo)致用戶退出登錄后,使用其他賬號登錄時,還保存著前一個用戶的信息,經(jīng)歷了一系列的暴力刪除無效后,最終采用了以下方法,用戶退出登陸后移除keep-alive:

<keep-alive exclude="Footbar,Navbar" v-if="isLoggedIn">
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
<router-view v-if="!$route.meta.keepAlive||!isLoggedIn"></router-view>
data() {
    return {
      isLoggedIn: false,
    };
  },
 watch: {
    $route(to, from) {
      // if the route changes...
      let token = localStorage.getItem("token")||''
      if (token) {
        // firebase returns null if user logged out
        this.isLoggedIn = true;
      } else {
        this.isLoggedIn = false;
      }
    }
  }

參考了:

https://stackoverflow.com/questions/48661595/how-to-destroy-a-vuejs-component-that-is-being-cached-by-keep-alive
https://zhuanlan.zhihu.com/p/40374425

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容