link137 link138 link139 link140 link141 link142 link143 link144 link145 link146 link147 link148 link149 link150 link151 link152 link153 link154 link155 link156 link157 link158 link159 link160 link161 link162 link163 link164 link165 link166 link167 link168 link169 link170 link171 link172 link173 link174 link175 link176 link177 link178 link179 link180 link181 link182 link183 link184 link185 link186 link187 link188 link189 link190 link191 link192 link193 link194 link195 link196 link197 link198 link199 link200 link201 link202 link203 link204 link205 link206 link207 link208 link209 link210 link211 link212 link213 link214 link215 link216 link217 link218 link219 link220 link221 link222 link223 link224 link225 link226 link227 link228 link229 link230 link231 link232 link233 link234 link235 link236 link237 link238 link239 link240 link241 link242 link243 link244 link245 link246 link247 link248 link249 link250 link251 link252 link253 link254 link255 link256 link257 link258 link259 link260 link261 link262 link263 link264 link265 link266 link267 link268 link269 link270 link271 link272 link273

[Vue.js] Click on VUE does not work correctly on component child Subscribe to RSS

I’m working with vue.js and there is a component called Quotation and inside I call another component (child component) called sideBarOptions which is a sidebar that is shown above the parent component with the option to hide it

From sideBarOptions there is a property called conditions set like this: conditions = false and to change it to true with a simple @click but it does not work immediately until I close sideBarOptions and at that moment the @click is triggered.

This is my code:

Quotation.vue

<template>
<sideBarOptions :showSidebar=show @closeSideBar=”closeSideBar”/>
</template>

export default {
data() {
return {
show: false,
}
},
methods: {
closeSideBar(value) {
this.show = value;
},
}
}

sideBarOptions.vue

<template>
<div @click=”conditions = !conditions”>
<input type=”checkbox” id=”box-1”>
<label for=”box-1”>Aceptar</label>
</div>

<div class=”sideBar”>
<a href=”#” class=”closeBtn-send-email” @click=”closeSideBar()”>
<h2>Envar Cotizacin</h2>
<span>X</span>
</a>
</div>
</template>

export default {
props: {
showSidebar: Boolean
},
data() {
return {
conditions: false,
}
},
methods: {
closeSideBar() {
this.$emit(‘closeSideBar’, false);
},
}
}

I dont understand the reason why it changes until I close the child component. Can you help me? Thank you.

Solution :

Try putting the model on the checkbox:

<div>
<input type=”checkbox” id=”box-1” v-model=”conditions”>
<label for=”box-1”>Aceptar</label>
</div>

also multiple elements in one component might be an issue, so wrap it in a div, presuming its just the example.

<template>
<div>
<div @click=”conditions = !conditions”>
<input type=”checkbox” id=”box-1”>
<label for=”box-1”>Aceptar</label>
</div>

<div class=”sideBar”>
<a href=”#” class=”closeBtn-send-email” @click=”closeSideBar()”>
<h2>Envar Cotizacin</h2>
<span>X</span>
</a>
</div>
</div>
</template>

Solution 2:

So far I can see everything works as expected:

if you click form - click on div triggers twice,

if you click div - click on div triggers once

var clickCount = 0;
function mouseDown(){
clickCount++;
}
document.addEventListener(“mousedown”, mouseDown);
new Vue({
components: {
sidebar: {
template: ‘#sidebar’,
data(){
return {
conditions: false
}
},
methods: {
conditionsChange(){
console.log(“do”, this.conditions, clickCount)
this.conditions = !this.conditions
}
}
}
}
}).$mount(‘#app’)
<script src=”https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id=”app”>
<sidebar />
</div>

<template id=”sidebar”>
<div @click=”conditionsChange”>
<input type=”checkbox” id=”box-1”>
<label for=”box-1”>Aceptar</label>
</div>
</template>