提交 | 用户 | 时间
|
820397
|
1 |
//outside.js |
H |
2 |
|
|
3 |
const ctx = '@@clickoutsideContext' |
|
4 |
|
|
5 |
export default { |
|
6 |
bind(el, binding, vnode) { |
|
7 |
const ele = el |
|
8 |
const documentHandler = (e) => { |
|
9 |
if (!vnode.context || ele.contains(e.target)) { |
|
10 |
return false |
|
11 |
} |
|
12 |
// 调用指令回调 |
|
13 |
if (binding.expression) { |
|
14 |
vnode.context[el[ctx].methodName](e) |
|
15 |
} else { |
|
16 |
el[ctx].bindingFn(e) |
|
17 |
} |
|
18 |
} |
|
19 |
// 将方法添加到ele |
|
20 |
ele[ctx] = { |
|
21 |
documentHandler, |
|
22 |
methodName: binding.expression, |
|
23 |
bindingFn: binding.value |
|
24 |
} |
|
25 |
|
|
26 |
setTimeout(() => { |
|
27 |
document.addEventListener('touchstart', documentHandler) // 为document绑定事件 |
|
28 |
}) |
|
29 |
}, |
|
30 |
update(el, binding) { |
|
31 |
const ele = el |
|
32 |
ele[ctx].methodName = binding.expression |
|
33 |
ele[ctx].bindingFn = binding.value |
|
34 |
}, |
|
35 |
unbind(el) { |
|
36 |
document.removeEventListener('touchstart', el[ctx].documentHandler) // 解绑 |
|
37 |
delete el[ctx] |
|
38 |
} |
|
39 |
} |