一个因@click.stop引发的bug
栏目: JavaScript · 发布时间: 7年前
内容简介:在项目页面中使用 element popover,设置在修复 bug 时,需要注意不会产生额外的 bug,那就需要了解修改的这段代码的含义从代码上看,点击 class 为 main 的 div 将会触发左边侧边栏缩略显示,加上 stop 修饰符是为了防止事件冒泡,所以能否去掉 stop 需要确认是否有这个必要。
在项目页面中使用 element popover,设置 trigger='click'
时点击外部不会触发自动隐藏,但在 element 官网中是可以正常触发的(官方示例),项目中的菜单是自定义写的,所以怀疑是有黑魔法。
查找原因
-
将 popover 写在
app.vue根组件内,发现可以正常触发自动隐藏。 -
在
app.vue的 mounted 钩子中加入window.addEventListener('click', () => console.log('window click===>>>>')),发现只有菜单栏外层能够触发。 -
检查菜单栏组件,发现代码中
<div class="main" @click.stop="isShowWhole = false">,这里的 click 事件使用了 stop 修饰符(阻止冒泡),可能阻止了 popover 外部点击的事件判断,尝试将 stop 修饰符去掉,发现外部点击事件正常触发。
确认代码修改没有副作用
在修复 bug 时,需要注意不会产生额外的 bug,那就需要了解修改的这段代码的含义
@click.stop="isShowWhole = false"
从代码上看,点击 class 为 main 的 div 将会触发左边侧边栏缩略显示,加上 stop 修饰符是为了防止事件冒泡,所以能否去掉 stop 需要确认是否有这个必要。
// router.js
let routes = [
{
path: '/',
alias: '/admin',
component: Menu,
children: [...Pages],
},
{
path: '*',
name: '404',
component: NotFound,
},
];
复制代码
在路由中可以看到,Menu 是作为根路由进行渲染,除了 404 页面都是它的子路由,所以 stop 修饰符是没有必要加上的,去除后经过测试没有其他影响。
深入 element popover 源码分析原因
对 element 组件进行 debug 时,可以直接引入相关组件的源码
import ElPopover from 'element-ui/packages/popover';
export default {
components: {
CheckboxFilter,
ElPopover
},
...
}
复制代码
然后我们就可以在 node_modules
的 element 源码进行 debug 操作( 危险步骤
,debug 后需要复原)。
// node_modules/element-ui/packages/popover/src/main.vue
mounted() {
...
if (this.trigger === 'click') {
on(reference, 'click', this.doToggle);
on(document, 'click', this.handleDocumentClick);
} else if (this.trigger === 'hover') {
...
} else if (this.trigger === 'focus') {
...
}
}
复制代码
popover 在 mounted 钩子内初始化了 trigger='click'
的事件绑定, on(document, 'click', this.handleDocumentClick)
这里绑定了 document 很可能就是阻止事件冒泡后不能触发外部点击隐藏的判断逻辑。
// node_modules/element-ui/packages/popover/src/main.vue
handleDocumentClick(e) {
let reference = this.reference || this.$refs.reference;
const popper = this.popper || this.$refs.popper;
if (!reference && this.$slots.reference && this.$slots.reference[0]) {
reference = this.referenceElm = this.$slots.reference[0].elm;
}
if (!this.$el ||
!reference ||
this.$el.contains(e.target) ||
reference.contains(e.target) ||
!popper ||
popper.contains(e.target)) return;
this.showPopper = false;
},
复制代码
这里判断 this.$el
是否包含 click 的 target,从而是否触发 this.showPopper = false
,当菜单栏阻止事件冒泡后 document 不能监听到 click 事件,才会无法进行外部点击隐藏的判断逻辑。
延伸v-clickoutside
element 的 select 组件中用到了 v-clickoutside 自定义指令,作用和 popover 的 handleDocumentClick 差不多(倒不如说 handleDocumentClick 是特殊的 clickoutside)
在上面的问题中,我们单独把 v-clickoutside 抽出来使用确实可以的,这是为什么呢?
// node_modules/element-ui/packages/popover/src/utils/clickoutside.js
!Vue.prototype.$isServer && on(document, 'mousedown', e => (startClick = e));
!Vue.prototype.$isServer && on(document, 'mouseup', e => {
nodeList.forEach(node => node[ctx].documentHandler(e, startClick));
});
复制代码
答案是 v-clickoutside 使用鼠标事件判断的,所以 click 的 阻止冒泡不会让 clickoutside 无效。
总结
解决 bug 的过程中需要做到不产生额外的 bug,并且深入分析问题的原因有助于能力的提高。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Ordering Disorder
Khoi Vinh / New Riders Press / 2010-12-03 / USD 29.99
The grid has long been an invaluable tool for creating order out of chaos for designers of all kinds—from city planners to architects to typesetters and graphic artists. In recent years, web designers......一起来看看 《Ordering Disorder》 这本书的介绍吧!
JS 压缩/解压工具
在线压缩/解压 JS 代码
Markdown 在线编辑器
Markdown 在线编辑器