[英]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.