I'm just wondering how to create a global function wherein it checks whether the localStorage
is not empty specifically if there's a token
inside
What I've tried is creating a global variable in my main.js
Vue.$checkIfTokenIsNotEmpty = !!localStorage.getItem('token');
// this returns true or false
In my component,
<template>
Is token Empty? {{ isTokenIsEmpty }} // I'm able to get true or false here
</template>
<script>
export default {
data(){
return {
isTokenIsEmpty: this.$checkIfTokenIsNotEmpty
}
}
}
</script>
This works properly when I reload the page. The problem is, it's not reactive or real time. If I clear the localStorage
, the value of my this.$checkIfTokenIsNotEmpty
doesn't change. It only changes when I reload the page which is bad in my spa vue project.