简体   繁体   English

Vue 为什么单击事件在 v-if 或 v-show 中不起作用

[英]Vue why click event not working in v-if or v-show

i am trying to fire click event from div but if v-if false on component rendering click event not working here my code:我正在尝试从 div 触发点击事件,但是如果组件渲染点击事件上v-if false ,我的代码在这里不起作用:

 export default { name: "ProSelect", data() { return { isActive: false, }}, methods: { select(event) { console.log('ID:' + event.currentTarget.id); } } }
 <div v-if="isActive" class="absolute shadow bg-white top-100 z-40 w-full lef-0 rounded max-h-select overflow-y-auto"> <div class="flex flex-col w-full"> <div id="foo" @click="select($event)" class="cursor-pointer w-full border-gray-100 rounded-t border-b hover:bg-teal-100 hover:bg-gray-100 hover:border-indigo-500"> <div class="flex w-full items-center p-2 pl-2 border-transparent border-l-2 relative hover:border-teal-100"> <div class="w-full items-center flex"> <div class="mx-2 -mt-1"> Jack jhon </div> </div> </div> </div> <div id="foo2" v-on:click="select($event)" class="cursor-pointer w-full border-gray-100 rounded-t border-b hover:bg-teal-100 hover:bg-gray-100 hover:border-indigo-500"> <div class="flex w-full items-center p-2 pl-2 border-transparent border-l-2 relative hover:border-teal-100"> <div class="w-full items-center flex"> <div class="mx-2 -mt-1"> Jack jhon 2 </div> </div> </div> </div> </div> </div>

if i change isActive variable to true on rendering click is working如果我在渲染点击时将isActive变量更改为true

i found the answer but i wonder why this is not working.If i use @mousedown.prevent instead of @click its working我找到了答案,但我想知道为什么这不起作用。如果我使用@mousedown.prevent而不是 @click 它的工作

I am not sure if I get it correctly but as per the code you posted.我不确定我是否正确理解,但根据您发布的代码。 Your parent div will be removed from the DOM as v-if is false.由于v-if为假,您的父 div 将从 DOM 中删除。 You can assign isActive as true while mounting.您可以在挂载时将isActive指定为true

Working Demo:工作演示:

 new Vue({ el: '#app', data() { return { isActive: false, } }, methods: { select(event) { console.log('ID:' + event.currentTarget.id); } }, mounted() { this.isActive = true; } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div v-if="isActive" class="absolute shadow bg-white top-100 z-40 w-full lef-0 rounded max-h-select overflow-y-auto"> <div class="flex flex-col w-full"> <div id="foo" @click="select($event)" class="cursor-pointer w-full border-gray-100 rounded-t border-b hover:bg-teal-100 hover:bg-gray-100 hover:border-indigo-500"> <div class="flex w-full items-center p-2 pl-2 border-transparent border-l-2 relative hover:border-teal-100"> <div class="w-full items-center flex"> <div class="mx-2 -mt-1"> Jack jhon </div> </div> </div> </div> <div id="foo2" v-on:click="select($event)" class="cursor-pointer w-full border-gray-100 rounded-t border-b hover:bg-teal-100 hover:bg-gray-100 hover:border-indigo-500"> <div class="flex w-full items-center p-2 pl-2 border-transparent border-l-2 relative hover:border-teal-100"> <div class="w-full items-center flex"> <div class="mx-2 -mt-1"> Jack jhon 2 </div> </div> </div> </div> </div> </div> </div>

this is whole template code这是整个模板代码

 <template> <div> <div class="flex flex-col items-center"> <div class="w-full flex flex-col items-center"> <div class="w-full"> <div class="flex flex-col items-center relative"> <div class="w-full"> <div class="flex"> <div class="flex flex-auto flex-wrap"></div> <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:rounded-none focus:rounded-t-lg focus:border-b-0 focus:bg-white focus:border-indigo-500 border-transparent focus:border-transparent focus:ring-0" type="text" v-model="selected":name="name" @focusin="isActive =:isActive" @focusout="isActive =:isActive" placeholder="Search by position"> </div> </div> <div v-if="isActive" class="absolute shadow bg-white top-100 z-40 w-full lef-0 rounded max-h-select overflow-y-auto"> <div class="flex flex-col w-full"> <div id="foo" @click="select($event)" class="cursor-pointer w-full border-gray-100 rounded-t border-b hover:bg-teal-100 hover:bg-gray-100 hover:border-indigo-500"> <div class="flex w-full items-center p-2 pl-2 border-transparent border-l-2 relative hover:border-teal-100"> <div class="w-full items-center flex"> <div class="mx-2 -mt-1"> Jack jhon </div> </div> </div> </div> <div id="foo2" v-on:click="select($event)" class="cursor-pointer w-full border-gray-100 rounded-t border-b hover:bg-teal-100 hover:bg-gray-100 hover:border-indigo-500"> <div class="flex w-full items-center p-2 pl-2 border-transparent border-l-2 relative hover:border-teal-100"> <div class="w-full items-center flex"> <div class="mx-2 -mt-1"> Jack jhon 2 </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 vue js 出错,v-show 或 v-if 出现问题 - Error in vue js, problems with v-show or v-if 更改组件按钮单击时的 Vue 根实例数据值,然后更改另一个组件的 v-show/v-if - Change Vue root instance data value on component's button click, then change another component's v-show/v-if vue.js 2 中哪个更好,使用 v-if 或 v-show? - What is better in vue.js 2, use v-if or v-show? 如何在 Vue.js 中将 v-show 和 v-if 嵌套在一起? - How to nest v-show and v-if together in Vue.js? 使用 setTimeout 函数时,在 Vue 中使用 v-show 或 v-if 切换可见性 - Toggle visibility using v-show or v-if in Vue while using a setTimeout function 使用 v-if 显示第一个渲染,然后使用 v-show? - Using v-if to show the first render, then v-show? Vue:使用数据控制DOM(但v-for,v-if,v-model和v-show不够用) - Vue: Use data to control DOM (but v-for, v-if, v-model, and v-show don't suffice) 为什么 v-show 和 v-if 会立即响应对象中的布尔值变化,而不是数组中的变化? - Why does v-show and v-if respond immediately to boolean changes in objects, but not in arrays? VueJS-v-if和v-show无法正常工作 - VueJS - v-if and v-show don't work properly 使用v-show隐藏元素并通过Vue中的keydown事件将其渲染回 - Hiding element with v-show and rendering it back with a keydown event in Vue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM