国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > 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;},            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 应用必备| 南宫市| 拉萨市| 东丰县| 嘉兴市| 西乌珠穆沁旗| 平果县| 越西县| 通州区| 彭水| 延川县| 满城县| 霍山县| 闸北区| 武夷山市| 台前县| 裕民县| 平果县| 襄垣县| 双桥区| 资源县| 介休市| 肥西县| 斗六市| 安阳县| 南丹县| 贵德县| 河津市| 镇宁| 新竹市| 岐山县| 四川省| 肇东市| 平遥县| 荥经县| 滨海县| 祥云县| 德昌县| 栾川县| 万源市| 宜黄县|