首页 > 语言 > JavaScript > 正文

一个因@click.stop引发的bug的解决

2024-05-06 15:43:26
字体:
来源:转载
供稿:网友

问题

在项目页面中使用 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.jslet 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.vuemounted() {  ...  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.vuehandleDocumentClick(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;},            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选