如何解決因@click.stop引發(fā)的bug問(wèn)題

這篇文章將為大家詳細(xì)講解有關(guān)如何解決因@click.stop引發(fā)的bug問(wèn)題,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),宜君網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:宜君等地區(qū)。宜君做網(wǎng)站價(jià)格咨詢:13518219792

問(wèn)題

在項(xiàng)目頁(yè)面中使用 element popover,設(shè)置trigger='click'時(shí)點(diǎn)擊外部不會(huì)觸發(fā)自動(dòng)隱藏,但在 element 官網(wǎng)中是可以正常觸發(fā)的(官方示例),項(xiàng)目中的菜單是自定義寫(xiě)的,所以懷疑是有黑魔法。

查找原因

  1. 將 popover 寫(xiě)在app.vue根組件內(nèi),發(fā)現(xiàn)可以正常觸發(fā)自動(dòng)隱藏。

  2. 在app.vue的 mounted 鉤子中加入window.addEventListener('click', () => console.log('window click===>>>>')),發(fā)現(xiàn)只有菜單欄外層能夠觸發(fā)。

  3. 檢查菜單欄組件,發(fā)現(xiàn)代碼中<div class="main" @click.stop="isShowWhole = false">,這里的 click 事件使用了 stop 修飾符(阻止冒泡),可能阻止了 popover 外部點(diǎn)擊的事件判斷,嘗試將 stop 修飾符去掉,發(fā)現(xiàn)外部點(diǎn)擊事件正常觸發(fā)。

確認(rèn)代碼修改沒(méi)有副作用

在修復(fù) bug 時(shí),需要注意不會(huì)產(chǎn)生額外的 bug,那就需要了解修改的這段代碼的含義

@click.stop="isShowWhole = false"

從代碼上看,點(diǎn)擊 class 為 main 的 div 將會(huì)觸發(fā)左邊側(cè)邊欄縮略顯示,加上 stop 修飾符是為了防止事件冒泡,所以能否去掉 stop 需要確認(rèn)是否有這個(gè)必要。

// router.js
let routes = [
  {
   path: '/',
   alias: '/admin',
   component: Menu,
   children: [...Pages],
  },
  {
   path: '*',
   name: '404',
   component: NotFound,
  },
 ];

在路由中可以看到,Menu 是作為根路由進(jìn)行渲染,除了 404 頁(yè)面都是它的子路由,所以 stop 修飾符是沒(méi)有必要加上的,去除后經(jīng)過(guò)測(cè)試沒(méi)有其他影響。

深入 element popover 源碼分析原因

對(duì) element 組件進(jìn)行 debug 時(shí),可以直接引入相關(guān)組件的源碼

import ElPopover from 'element-ui/packages/popover';
export default {
  components: {
    CheckboxFilter,
    ElPopover
  },
  ...
}

然后我們就可以在node_modules的 element 源碼進(jìn)行 debug 操作(危險(xiǎn)步驟,debug 后需要復(fù)原)。

// 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ā)外部點(diǎn)擊隱藏的判斷邏輯。

// 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,當(dāng)菜單欄阻止事件冒泡后 document 不能監(jiān)聽(tīng)到 click 事件,才會(huì)無(wú)法進(jìn)行外部點(diǎn)擊隱藏的判斷邏輯。

延伸v-clickoutside

element 的 select 組件中用到了 v-clickoutside 自定義指令,作用和 popover 的 handleDocumentClick 差不多(倒不如說(shuō) handleDocumentClick 是特殊的 clickoutside)

在上面的問(wèn)題中,我們單獨(dú)把 v-clickoutside 抽出來(lái)使用確實(shí)可以的,這是為什么呢?

// 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 使用鼠標(biāo)事件判斷的,所以 click 的 阻止冒泡不會(huì)讓 clickoutside 無(wú)效。

關(guān)于“如何解決因@click.stop引發(fā)的bug問(wèn)題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

當(dāng)前題目:如何解決因@click.stop引發(fā)的bug問(wèn)題
網(wǎng)站網(wǎng)址:http://muchs.cn/article32/gpjjsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、虛擬主機(jī)、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站排名、標(biāo)簽優(yōu)化、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站