0👍
✅
The problem is that computed properties will only update automatically if their dependant values are reactive. Basically Vue has no way of knowing when the values in localStorage have updated. There are libraries that will make this possible. However, without the use of such:
-
In your template change
v-if="!hasToken"
tov-if="!loggedIn"
-
modify your script
export default { data() { return { loggedIn: !!localStorage.getItem("token"); }; }, methods: { logOut() { localStorage.removeItem("token"); localStorage.removeItem("user"); localStorage.removeItem("id"); this.loggedIn = false; }, }, };
Source:stackexchange.com