只需设置一次自定义指令即可很好地解决:
Vue.directive('click-outside', { bind () { this.event = event => this.vm.$emit(this.expression, event) this.el.addEventListener('click', this.stopProp) document.body.addEventListener('click', this.event) }, unbind() { this.el.removeEventListener('click', this.stopProp) document.body.removeEventListener('click', this.event) }, stopProp(event) { event.stopPropagation() }})用法:
<div v-click-outside="nameOfCustomEventToCall"> Some content</div>
在组件中:
events: { nameOfCustomEventToCall: function (event) { // do something - probably hide the dropdown menu / modal etc. }}


