mirror of
https://github.com/youzan/vant.git
synced 2025-10-20 18:54:24 +00:00
fix: change utils && mixins alias
This commit is contained in:
44
packages/utils/clickoutside.js
Normal file
44
packages/utils/clickoutside.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* v-clickoutside
|
||||
* @desc 点击元素外面才会触发的事件
|
||||
* @example
|
||||
* ```vue
|
||||
* <div v-clickoutside="handleClose">
|
||||
* ```
|
||||
*/
|
||||
import Vue from 'vue';
|
||||
const isServer = Vue.prototype.$isServer;
|
||||
const clickoutsideContext = '@@clickoutsideContext';
|
||||
|
||||
export default {
|
||||
bind(el, binding, vnode) {
|
||||
const documentHandler = function(e) {
|
||||
if (vnode.context && !el.contains(e.target)) {
|
||||
vnode.context[el[clickoutsideContext].methodName]();
|
||||
}
|
||||
};
|
||||
el[clickoutsideContext] = {
|
||||
documentHandler,
|
||||
methodName: binding.expression,
|
||||
arg: binding.arg || 'click'
|
||||
};
|
||||
!isServer && document.addEventListener(el[clickoutsideContext].arg, documentHandler);
|
||||
},
|
||||
|
||||
update(el, binding) {
|
||||
el[clickoutsideContext].methodName = binding.expression;
|
||||
},
|
||||
|
||||
unbind(el) {
|
||||
!isServer && document.removeEventListener(
|
||||
el[clickoutsideContext].arg,
|
||||
el[clickoutsideContext].documentHandler);
|
||||
},
|
||||
|
||||
install(Vue) {
|
||||
Vue.directive('clickoutside', {
|
||||
bind: this.bind,
|
||||
unbind: this.unbind
|
||||
});
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user