你如何在 vuejs 中正确处理元素外的点击?
How do you handle click-outside of element properly in vuejs?
是否有任何适当的解决方案来处理元素外的点击?
那里有通用的解决方案,比如 Handling clicks outside an element without jquery :
window.onload = function() {
// For clicks inside the element
document.getElementById('myElement').onclick = function(e) {
// Make sure the event doesn't bubble from your element
if (e) { e.stopPropagation(); }
else { window.event.cancelBubble = true; }
// Place the code for this element here
alert('this was a click inside');
};
// For clicks elsewhere on the page
document.onclick = function() {
alert('this was a click outside');
};
};
但问题是几乎所有项目在不同的组件中都有多个不同的弹出窗口,我应该处理它们的外部点击。
我应该如何在不使用全局 window.on 的情况下处理点击事件?(我认为不可能将所有组件放在 window.on 中)
在努力解决这个问题并搜索之后,我找到了如何使用 vuejs 指令解决这个问题而不会流血:
1。使用库:
v-click-outside
不错,
https://www.npmjs.com/package/v-click-outside
2。没有图书馆:
```
//main.js
import '@/directives';
......
// directives.js
import Vue from "vue";
Vue.directive('click-outside', {
bind: function (element, binding, vnode) {
element.clickOutsideEvent = function (event) { // check that click was outside the el and his children
if (!(element === event.target || element.contains(event.target))) { // and if it did, call method provided in attribute value
vnode.context[binding.expression](event);
// binding.value(); run the arg
}
};
document.body.addEventListener('click', element.clickOutsideEvent)
},
unbind: function (element) {
document.body.removeEventListener('click', element.clickOutsideEvent)
}
});
```
通过 v-click-outside 指令在任何地方使用它,如下所示:
//header.vue
<div class="profileQuickAction col-lg-4 col-md-12" v-click-outside="hidePopUps">
...
</>
你可以在
上查看
是否有任何适当的解决方案来处理元素外的点击?
那里有通用的解决方案,比如 Handling clicks outside an element without jquery :
window.onload = function() {
// For clicks inside the element
document.getElementById('myElement').onclick = function(e) {
// Make sure the event doesn't bubble from your element
if (e) { e.stopPropagation(); }
else { window.event.cancelBubble = true; }
// Place the code for this element here
alert('this was a click inside');
};
// For clicks elsewhere on the page
document.onclick = function() {
alert('this was a click outside');
};
};
但问题是几乎所有项目在不同的组件中都有多个不同的弹出窗口,我应该处理它们的外部点击。
我应该如何在不使用全局 window.on 的情况下处理点击事件?(我认为不可能将所有组件放在 window.on 中)
在努力解决这个问题并搜索之后,我找到了如何使用 vuejs 指令解决这个问题而不会流血:
1。使用库:
v-click-outside
不错,
https://www.npmjs.com/package/v-click-outside
2。没有图书馆:
```
//main.js
import '@/directives';
......
// directives.js
import Vue from "vue";
Vue.directive('click-outside', {
bind: function (element, binding, vnode) {
element.clickOutsideEvent = function (event) { // check that click was outside the el and his children
if (!(element === event.target || element.contains(event.target))) { // and if it did, call method provided in attribute value
vnode.context[binding.expression](event);
// binding.value(); run the arg
}
};
document.body.addEventListener('click', element.clickOutsideEvent)
},
unbind: function (element) {
document.body.removeEventListener('click', element.clickOutsideEvent)
}
});
```
通过 v-click-outside 指令在任何地方使用它,如下所示:
//header.vue
<div class="profileQuickAction col-lg-4 col-md-12" v-click-outside="hidePopUps">
...
</>
你可以在
上查看