一個因@click.stop引發(fā)的bug的解決
問題
在項目頁面中使用 element popover,設置trigger='click'時點擊外部不會觸發(fā)自動隱藏,但在 element 官網(wǎng)中是可以正常觸發(fā)的(官方示例),項目中的菜單是自定義寫的,所以懷疑是有黑魔法。
查找原因
- 將 popover 寫在app.vue根組件內(nèi),發(fā)現(xiàn)可以正常觸發(fā)自動隱藏。
- 在app.vue的 mounted 鉤子中加入
window.addEventListener('click', () => console.log('window click===>>>>')),發(fā)現(xiàn)只有菜單欄外層能夠觸發(fā)。 - 檢查菜單欄組件,發(fā)現(xiàn)代碼中
<div class="main" @click.stop="isShowWhole = false">,這里的 click 事件使用了 stop 修飾符(阻止冒泡),可能阻止了 popover 外部點擊的事件判斷,嘗試將 stop 修飾符去掉,發(fā)現(xiàn)外部點擊事件正常觸發(fā)。
確認代碼修改沒有副作用
在修復 bug 時,需要注意不會產(chǎn)生額外的 bug,那就需要了解修改的這段代碼的含義
@click.stop="isShowWhole = false"
從代碼上看,點擊 class 為 main 的 div 將會觸發(fā)左邊側(cè)邊欄縮略顯示,加上 stop 修飾符是為了防止事件冒泡,所以能否去掉 stop 需要確認是否有這個必要。
// router.js
let routes = [
{
path: '/',
alias: '/admin',
component: Menu,
children: [...Pages],
},
{
path: '*',
name: '404',
component: NotFound,
},
];
在路由中可以看到,Menu 是作為根路由進行渲染,除了 404 頁面都是它的子路由,所以 stop 修飾符是沒有必要加上的,去除后經(jīng)過測試沒有其他影響。
深入 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 鉤子內(nèi)初始化了trigger='click'的事件綁定,on(document, 'click', this.handleDocumentClick)這里綁定了 document 很可能就是阻止事件冒泡后不能觸發(fā)外部點擊隱藏的判斷邏輯。
// 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,從而是否觸發(fā)this.showPopper = false,當菜單欄阻止事件冒泡后 document 不能監(jiān)聽到 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 無效。
總結(jié)
解決 bug 的過程中需要做到不產(chǎn)生額外的 bug,并且深入分析問題的原因有助于能力的提高。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue項目中安裝依賴npm?install一直報錯的解決過程
這篇文章主要給大家介紹了關于Vue項目中安裝依賴npm?install一直報錯的解決過程,要解決NPM安裝依賴報錯,首先要分析出錯誤的原因,文中將解決的過程介紹的非常詳細,需要的朋友可以參考下2023-05-05
vue3?ts編寫echart是tooltip無法展示的解決
這篇文章主要介紹了vue3?ts編寫echart是tooltip無法展示的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
使用Element-UI的el-tabs組件,瀏覽器卡住了的問題及解決
這篇文章主要介紹了使用Element-UI的el-tabs組件,瀏覽器卡住了的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
實例詳解Vue項目使用eslint + prettier規(guī)范代碼風格
這篇文章主要介紹了Vue項目使用eslint + prettier規(guī)范代碼風格,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-08-08

