admin管理员组文章数量:1426236
Vue JS 2.6.10
I have read a variety of SO posts about how to create a custom directive so that you can detect a click outside of a popup menu so you can close it. I can't quite get it to work because I have a button that opens the menu and clicking it fires the "close" behavior.
Here's my main view Logbook.vue
that has the button that opens the menu:
// --- Logbook.vue ---
<script>
export default {
name: 'Logbook',
ponents:{
Years
},
methods:{
clickYears: function(){
this.$refs.Years.show = true
}
}
}
</script>
<template>
<div id="current-year">
<a href="#year" ref="yearButton" v-on:click.prevent="clickYears">{{ currentYear }}</a>
<Years ref="Years" v-on:selectYear="yearSelected" />
</div>
</template>
Here is the menu ponent Years.vue
that opens when you click the button:
//--- Years.vue ---
<script>
import Vue from 'vue'
//Custom directive to handle clicks outside of this ponent
Vue.directive('click-outside', {
bind: function (el, binding, vnode) {
window.event = function (event) {
if (!(el == event.target || el.contains(event.target))) {
vnode.context[binding.expression](event)
}
};
document.body.addEventListener('click', window.event)
},
unbind: function (el) {
document.body.removeEventListener('click', window.event)
}
})
export default{
name: 'Years',
data() {
return {
show: false
}
},
methods:{
close: function(){
this.show = false
}
}
}
</script>
<template>
<div id="years" v-show="show" v-click-outside="close">
<!-- Years listed here... -->
</div>
</template>
The close
method is firing appropriately when I click outside of the Years
ponent, but the problem is that I can't ever open the Years
menu to begin with because clicking the button also fires the close
behavior because it's also outside of the Years ponent.
Has anyone overe this problem? Any ideas?
Vue JS 2.6.10
I have read a variety of SO posts about how to create a custom directive so that you can detect a click outside of a popup menu so you can close it. I can't quite get it to work because I have a button that opens the menu and clicking it fires the "close" behavior.
Here's my main view Logbook.vue
that has the button that opens the menu:
// --- Logbook.vue ---
<script>
export default {
name: 'Logbook',
ponents:{
Years
},
methods:{
clickYears: function(){
this.$refs.Years.show = true
}
}
}
</script>
<template>
<div id="current-year">
<a href="#year" ref="yearButton" v-on:click.prevent="clickYears">{{ currentYear }}</a>
<Years ref="Years" v-on:selectYear="yearSelected" />
</div>
</template>
Here is the menu ponent Years.vue
that opens when you click the button:
//--- Years.vue ---
<script>
import Vue from 'vue'
//Custom directive to handle clicks outside of this ponent
Vue.directive('click-outside', {
bind: function (el, binding, vnode) {
window.event = function (event) {
if (!(el == event.target || el.contains(event.target))) {
vnode.context[binding.expression](event)
}
};
document.body.addEventListener('click', window.event)
},
unbind: function (el) {
document.body.removeEventListener('click', window.event)
}
})
export default{
name: 'Years',
data() {
return {
show: false
}
},
methods:{
close: function(){
this.show = false
}
}
}
</script>
<template>
<div id="years" v-show="show" v-click-outside="close">
<!-- Years listed here... -->
</div>
</template>
The close
method is firing appropriately when I click outside of the Years
ponent, but the problem is that I can't ever open the Years
menu to begin with because clicking the button also fires the close
behavior because it's also outside of the Years ponent.
Has anyone overe this problem? Any ideas?
Share Improve this question asked May 20, 2019 at 22:58 Clifton LabrumClifton Labrum 14.2k13 gold badges79 silver badges156 bronze badges1 Answer
Reset to default 5try this
...
methods:{
clickYears: function(event){
this.$refs.Years.show = true
event.stopPropagation();
}
}
...
本文标签:
版权声明:本文标题:javascript - Vue JS: Open Menu Component with Button, Close with Click Outside of Menu - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745472047a2659793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论