user3118789
user3118789

Reputation: 609

v-if on Vuex getter

I am using a vuex getters isLoggedIn to check if a user is logged in or not.

<div v-if="isLoggedIn" class="ml-2 py-2 group relative">...</div>
data() {
            return {
                isLoggedIn: this.$store.getters['auth/isLoggedIn']
            }
        },

It doesn't work. But If I don't set the value of getters to data and check the condition directly in v-if instead of, it will work. Something like this:

// This works for me.
<div v-if="this.$store.getters['auth/isLoggedIn']" class="ml-2 py-2 group relative">...</div>

I don't know what is the difference? Thank you.

Upvotes: 1

Views: 2989

Answers (1)

apple apple
apple apple

Reputation: 10624

use computed, data only execute once

computed:{ 
   isLoggedIn(){return this.$store.getters['auth/isLoggedIn']} 
}

Upvotes: 5

Related Questions