The purpose of this code is to show and hide some facebook-like dropdown mini-tabs by toggling data. The problem is it called every toggling function on a given click event, but some of them updating data variables some are don't. E.G: I called toggleUser()
in 2 different places. However, first changes the data variable and re-render UI, a second line called on class="avatar pointer"
is not toggling data variable but calls a function.
<template>
<div class="flex header">
<div class="nav-item">
<div class="notification pointer" @click="toggleNotif" >
<small class="notif-count"> {{notif_count}}
</small>
</div>
<div class="notif-tab" v-show="showNotif">
<p class="tab-title flex" >Notification</p>
</div>
</div>
<div class="nav-item ">
<div class="flex">
<div class="user pointer" v-on:click="toggleUser">userName <i class="arrow down"></i></div>
<div class="avatar pointer" v-on:click="toggleUser" v-bind:style="{ backgroundImage: 'url('+ avatar +')' }"></div>
</div>
<div class="user-tab" v-show="showUser">
</div>
</div>
<div class="nav-item">
<div class="cart pointer" @click="toggleCart">
<small class="cart-count"> <div>{{cart_count}} </div>
</small>
</div>
<div class="cart-tab" v-show="showCart">
<div v-on:click="logout()" class="pointer">
Logout
</div>
</div>
</div>
</div>
</template>
export default {
name: 'TTHeader',
data: () => {
return {
showNotif: false,
showUser: false,
showCart: false,
notif_count: 0,
cart_count: 0,
avatar: "https://cdn.iconscout.com/icon/free/png-256/avatar-370-456322.png",
}
},
methods: {
toggleNotif() {
this.showNotif = !this.showNotif;
this.showUser = false;
this.showCart = false;
},
toggleUser() {
this.showUser = !this.showUser;
this.showNotif = false;
this.showCart = false;
},
toggleCart() {
this.showCart = !this.showCart;
this.showUser = false;
this.showNotif = false;
},
close (e) {
if (!["user-tab", "notif-tab", "cart-tab", "bell pointer", "user pointer", "cart pointer", "user pointer"].includes(e.target.className)) {
this.showCart = false;
this.showUser = false;
this.showNotif = false;
}
}
},
mounted () {
document.addEventListener('click', this.close)
},
beforeDestroy () {
document.removeEventListener('click',this.close)
}
}