亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3之事件綁定的實現(xiàn)邏輯詳解

 更新時間:2023年11月15日 09:42:47   作者:JonnyLan  
這篇文章主要介紹了Vue3之事件綁定的實現(xiàn)邏輯,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Vue的事件綁定主要是通過v-on指令來實現(xiàn)的,這個指令既可以實現(xiàn)原生事件綁定,例如onclick等。

也可以實現(xiàn)組件的自定義事件,從而實現(xiàn)組件的數(shù)據(jù)通信。

本文我們就來分析下Vue的事件處理的邏輯。

v-on作用于普通元素

用在普通元素上時,只能監(jiān)聽原生 DOM 事件,最多的就是onclick事件了。

我們就以onclick事件來分析原理。

案例

let click = () => {
  console.log("點擊我,很快樂")
};

<!-- template -->
<div v-on:click="click">點擊我吧</div>

分析實現(xiàn)邏輯

我們先來看下渲染函數(shù)

const _hoisted_1 = ["onClick"]

function render(_ctx, _cache) {
  with (_ctx) {
    const { openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue

    return (_openBlock(), _createElementBlock("div", { onClick: click }, "點擊我吧", 8 /* PROPS */, _hoisted_1))
  }
}

我們看到 渲染函數(shù)在創(chuàng)建VNode的時候傳了一個onClickpros;

我們先來看下patchProp函數(shù)中對onClick這個pros的處理邏輯

export const patchProp: DOMRendererOptions['patchProp'] = (
  el,
  key,
  prevValue,
  nextValue,
  isSVG = false,
  prevChildren,
  parentComponent,
  parentSuspense,
  unmountChildren
) => {
  if (isOn(key)) {
    patchEvent(el, key, prevValue, nextValue, parentComponent)
  }
}

function patchEvent(el, rawName, prevValue, nextValue, instance = null) {
  // vei = vue event invokers
  const invokers = el._vei || (el._vei = {});
  
  if (添加) {
    const invoker = (invokers[rawName] = createInvoker(nextValue, instance));
    el.addEventListener(event, handler, options)
  } else {
    el.removeEventListener(event, handler, options)
  }
}

我們可以看出來底層就是調(diào)用的addEventListener函數(shù)進(jìn)行事件監(jiān)聽綁定,調(diào)用removeEventListener進(jìn)行事件監(jiān)聽解綁。

其實這個實現(xiàn)邏輯很容易想到,沒什么難度。

重點分析—事件修飾符

once,capturepassive

這兩個可以直接作為addEventListenerremoveEventListener 的第三個參數(shù)options 中的值,因為這是W3C支持的事件可選參數(shù)。

stop, prevent,capture, self等。

這類修飾符被封裝在另外一個withModifiers函數(shù)中。

export const withModifiers = (fn: Function, modifiers: string[]) => {
  return (event: Event, ...args: unknown[]) => {
    for (let i = 0; i < modifiers.length; i++) {
      const guard = modifierGuards[modifiers[i]]
      if (guard && guard(event, modifiers)) return
    }
    return fn(event, ...args)
  }
}

這里設(shè)計的非常精妙,每個修飾符都對應(yīng)一個執(zhí)行函數(shù),如果調(diào)用執(zhí)行函數(shù)guard(event, modifiers)返回true, 則函數(shù)withModifiers就直接返回了,不會再執(zhí)行事件的函數(shù)fn(event, ...args)了。

這里列一些這些修飾符對應(yīng)的函數(shù):

const modifierGuards: Record<
  string,
  (e: Event, modifiers: string[]) => void | boolean
> = {
  stop: e => e.stopPropagation(),
  prevent: e => e.preventDefault(),
  self: e => e.target !== e.currentTarget,
  ctrl: e => !(e as KeyedEvent).ctrlKey,
  shift: e => !(e as KeyedEvent).shiftKey,
  alt: e => !(e as KeyedEvent).altKey,
  meta: e => !(e as KeyedEvent).metaKey,
  left: e => 'button' in e && (e as MouseEvent).button !== 0,
  middle: e => 'button' in e && (e as MouseEvent).button !== 1,
  right: e => 'button' in e && (e as MouseEvent).button !== 2,
  exact: (e, modifiers) =>
    systemModifiers.some(m => (e as any)[`${m}Key`] && !modifiers.includes(m))
}

v-on作用于組件綁定自定義事件

實現(xiàn)案例

父組件中 有個子組件son, 使用v-on綁定了子組件的自定義事件,還有一個p顯示當(dāng)前的時間戳。

<Son v-on:children-clicked="childClickedHandler" />
<p>{{ date }}</p>

setup() {

  let childClickedHandler = (data: Date) => {
    date.value = data.getTime();
  }

  let date = ref(new Date().getTime());

  return {
    date,
    childClickedHandler
  };
},

子組件中有一個div, 每次點擊會觸發(fā)自定義事件childrenClicked, 并且傳遞了一個參數(shù)值為當(dāng)前時間。

<div v-on:click="clickevent">點擊我吧</div>

emits: ["childrenClicked"],
setup(props, {emit}) {

  let clickevent = () => {
    emit('childrenClicked', new Date());
  }
  return {clickevent};
},            

這樣點擊子組件后就會觸發(fā)父組件的childClickedHandler方法,從而更新當(dāng)前時間戳的顯示。

接下來我們就來看看這底層的邏輯是如何實現(xiàn)的?

實現(xiàn)邏輯

先看下兩個組件的渲染函數(shù)的重點部分

父組件:

_createVNode(_component_Son, { onChildrenClicked: childClickedHandler }, null, 8 /* PROPS */, ["onChildrenClicked"])

父組件給子組件綁定自定義事件是傳遞了一個事件pro,這個pro的名稱用駝峰命名, 例如本例中的onChildrenClicked。

子組件:

const _hoisted_1 = ["onClick"]

_createElementBlock("div", {
    onClick: $event => ($emit('childrenClicked', new Date()))
}, "點擊我吧", 8 /* PROPS */, _hoisted_1)

子組件div點擊的綁定前面說過,點擊的時候執(zhí)行$emit('childrenClicked', new Date(), 這個沒有什么特別的。

現(xiàn)在的問題就是為什么子組件$emit('childrenClicked', new Date()如何找到父組件的onChildrenClicked方法并執(zhí)行?

$emit來自于createSetupContext函數(shù)調(diào)用時候傳入的參數(shù)setupContext

export function createSetupContext(
  instance: ComponentInternalInstance
): SetupContext {
    return {
      get attrs() {
        return attrs || (attrs = createAttrsProxy(instance))
      },
      slots: instance.slots,
      emit: instance.emit,
      expose
    }
  }
}

$emit就是組件實例的emit方法。

實例的emit方法用于尋找對應(yīng)的自定義事件的函數(shù)

export function emit(
  instance: ComponentInternalInstance,
  event: string,
  ...rawArgs: any[]
) {
  const props = instance.vnode.props || EMPTY_OBJ

  // 傳入的傳參
  let args = rawArgs
  
  // TODO: 處理v-mode的方法
  const isModelListener = event.startsWith('update:')

  // 處理函數(shù)名,on+首字母大寫的函數(shù)名 或者 on+駝峰命名的函數(shù)名 
  let handlerName
  let handler =
    props[(handlerName = toHandlerKey(event))] ||
    props[(handlerName = toHandlerKey(camelize(event)))]
  if (!handler && isModelListener) {
    handler = props[(handlerName = toHandlerKey(hyphenate(event)))]
  }

  if (handler) {
    // 調(diào)用函數(shù),參數(shù)是外部傳入的參數(shù)
    callWithAsyncErrorHandling(
      handler,
      instance,
      ErrorCodes.COMPONENT_EVENT_HANDLER,
      args
    )
  }
  
}

如果函數(shù)名以update:開頭,說明是一個v-model的修改數(shù)據(jù)函數(shù),這部分邏輯會在v-model專門的文章中介紹;

然后在實例對象的props中找 on+首字母大寫的函數(shù)名 的函數(shù),如果沒找到,則找 on+首字母大寫且駝峰命名的函數(shù)名 的函數(shù);

如果找到了對應(yīng)的函數(shù),則調(diào)用函數(shù),調(diào)用函數(shù)的參數(shù)為傳入的參數(shù)。

總結(jié)

v-on作用于普通元素底層是利用 addEventListenerremoveEventListener,修飾符要么利用W3C標(biāo)準(zhǔn),要么利用函數(shù)調(diào)用來實現(xiàn);

v-on作用于組件是 子組件利用 emitpro 中搜尋到對應(yīng)的函數(shù)(由父組件傳入),然后執(zhí)行對應(yīng)的函數(shù)。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動態(tài)合并方式

    vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動態(tài)合并方式

    這篇文章主要介紹了vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動態(tài)合并方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 使用Vue3實現(xiàn)一個簡單的思維導(dǎo)圖組件

    使用Vue3實現(xiàn)一個簡單的思維導(dǎo)圖組件

    思維導(dǎo)圖是一種用于表示信息、想法和概念的圖形化工具,本文將基于 Vue3和VueDraggable實現(xiàn)一個簡單的思維導(dǎo)圖組件,支持節(jié)點拖拽,編輯及節(jié)點之間的關(guān)系連接,希望對大家有所幫助
    2025-04-04
  • Vue SPA 首屏優(yōu)化方案

    Vue SPA 首屏優(yōu)化方案

    這篇文章主要介紹了Vue SPA 首屏優(yōu)化方案的的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下
    2021-02-02
  • vue如何遍歷data所有變量并賦值

    vue如何遍歷data所有變量并賦值

    這篇文章主要介紹了vue如何遍歷data所有變量并賦值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue3實現(xiàn)虛擬列表的示例代碼

    Vue3實現(xiàn)虛擬列表的示例代碼

    虛擬列表是一種優(yōu)化長列表渲染的技術(shù),它可以在保持流暢性的同時,渲染大量的數(shù)據(jù),本文主要介紹了如何通過Vue3實現(xiàn)一個虛擬列表,感興趣的可以了解下
    2024-11-11
  • elementui彈窗頁按鈕重復(fù)提交問題解決方法

    elementui彈窗頁按鈕重復(fù)提交問題解決方法

    本文主要介紹了elementui彈窗頁按鈕重復(fù)提交問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • 詳解vue-router 路由元信息

    詳解vue-router 路由元信息

    本篇文章主要介紹了vue-router 路由元信息,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue-Cli配置代理轉(zhuǎn)發(fā)解決跨域問題的方法

    Vue-Cli配置代理轉(zhuǎn)發(fā)解決跨域問題的方法

    本文主要介紹了Vue-Cli配置代理轉(zhuǎn)發(fā)解決跨域問題的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • vue 集成jTopo 處理方法

    vue 集成jTopo 處理方法

    這篇文章主要介紹了vue 集成jTopo 處理方法,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-08-08
  • 解讀vant的Uploader上傳問題

    解讀vant的Uploader上傳問題

    這篇文章主要介紹了解讀vant的Uploader上傳問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論