最近項目中被一個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