日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Vue3.0進(jìn)階之自定義事件探秘

這是 Vue 3.0 進(jìn)階系列 的第二篇文章,該系列的第一篇文章是 Vue 3.0 進(jìn)階之指令探秘。本文阿寶哥將以一個(gè)簡(jiǎn)單的示例為切入點(diǎn),帶大家一起一步步揭開(kāi)自定義事件背后的秘密。

 
 
 
 
  • 在以上示例中,我們先通過(guò) Vue.createApp 方法創(chuàng)建 app 對(duì)象,之后利用該對(duì)象上的 component 方法注冊(cè)全局組件 —— welcome-button 組件。在定義該組件時(shí),我們通過(guò) emits 屬性定義了該組件上的自定義事件。當(dāng)然用戶點(diǎn)擊 歡迎 按鈕時(shí),就會(huì)發(fā)出 welcome 事件,之后就會(huì)調(diào)用 sayHi 方法,接著控制臺(tái)就會(huì)輸出 你好,我是阿寶哥! 。

    雖然該示例比較簡(jiǎn)單,但也存在以下 2 個(gè)問(wèn)題:

    • $emit 方法來(lái)自哪里?
    • 自定義事件的處理流程是什么?

    下面我們將圍繞這些問(wèn)題來(lái)進(jìn)一步分析自定義事件背后的機(jī)制,首先我們先來(lái)分析第一個(gè)問(wèn)題。

    一、$emit 方法來(lái)自哪里?

    使用 Chrome 開(kāi)發(fā)者工具,我們?cè)?sayHi 方法內(nèi)部加個(gè)斷點(diǎn),然后點(diǎn)擊 歡迎 按鈕,此時(shí)函數(shù)調(diào)用棧如下圖所示:

    在上圖右側(cè)的調(diào)用棧,我們發(fā)現(xiàn)了一個(gè)存在于 componentEmits.ts 文件中的 emit 方法。但在模板中,我們使用的是 $emit 方法,為了搞清楚這個(gè)問(wèn)題,我們來(lái)看一下 onClick 方法:

    由上圖可知,我們的 $emit 方法來(lái)自 _ctx 對(duì)象,那么該對(duì)象是什么對(duì)象呢?同樣,利用斷點(diǎn)我們可以看到 _ctx 對(duì)象的內(nèi)部結(jié)構(gòu):

    很明顯 _ctx 對(duì)象是一個(gè) Proxy 對(duì)象,如果你對(duì) Proxy 對(duì)象還不了解,可以閱讀 你不知道的 Proxy 這篇文章。當(dāng)訪問(wèn) _ctx 對(duì)象的 $emit 屬性時(shí),將會(huì)進(jìn)入 get 捕獲器,所以接下來(lái)我們來(lái)分析 get 捕獲器:

    通過(guò) [[FunctionLocation]] 屬性,我們找到了 get 捕獲器的定義,具體如下所示:

     
     
     
     
    1. // packages/runtime-core/src/componentPublicInstance.ts
    2. export const RuntimeCompiledPublicInstanceProxyHandlers = extend(
    3.   {},
    4.   PublicInstanceProxyHandlers,
    5.   {
    6.     get(target: ComponentRenderContext, key: string) {
    7.       // fast path for unscopables when using `with` block
    8.       if ((key as any) === Symbol.unscopables) {
    9.         return
    10.       }
    11.       return PublicInstanceProxyHandlers.get!(target, key, target)
    12.     },
    13.     has(_: ComponentRenderContext, key: string) {
    14.       const has = key[0] !== '_' && !isGloballyWhitelisted(key)
    15.       // 省略部分代碼
    16.       return has
    17.     }
    18.   }
    19. )

    觀察以上代碼可知,在 get 捕獲器內(nèi)部會(huì)繼續(xù)調(diào)用 PublicInstanceProxyHandlers 對(duì)象的 get 方法來(lái)獲取 key 對(duì)應(yīng)的值。由于 PublicInstanceProxyHandlers 內(nèi)部的代碼相對(duì)比較復(fù)雜,這里我們只分析與示例相關(guān)的代碼:

     
     
     
     
    1. // packages/runtime-core/src/componentPublicInstance.ts
    2. export const PublicInstanceProxyHandlers: ProxyHandler = {
    3.   get({ _: instance }: ComponentRenderContext, key: string) {
    4.     const { ctx, setupState, data, props, accessCache, type, appContext } = instance
    5.    
    6.     // 省略大部分內(nèi)容
    7.     const publicGetter = publicPropertiesMap[key]
    8.     // public $xxx properties
    9.     if (publicGetter) {
    10.       if (key === '$attrs') {
    11.         track(instance, TrackOpTypes.GET, key)
    12.         __DEV__ && markAttrsAccessed()
    13.       }
    14.       return publicGetter(instance)
    15.     },
    16.     // 省略set和has捕獲器
    17. }

    在上面代碼中,我們看到了 publicPropertiesMap 對(duì)象,該對(duì)象被定義在 componentPublicInstance.ts 文件中:

     
     
     
     
    1. // packages/runtime-core/src/componentPublicInstance.ts
    2. const publicPropertiesMap: PublicPropertiesMap = extend(Object.create(null), {
    3.   $: i => i,
    4.   $el: i => i.vnode.el,
    5.   $data: i => i.data,
    6.   $props: i => (__DEV__ ? shallowReadonly(i.props) : i.props),
    7.   $attrs: i => (__DEV__ ? shallowReadonly(i.attrs) : i.attrs),
    8.   $slots: i => (__DEV__ ? shallowReadonly(i.slots) : i.slots),
    9.   $refs: i => (__DEV__ ? shallowReadonly(i.refs) : i.refs),
    10.   $parent: i => getPublicInstance(i.parent),
    11.   $root: i => getPublicInstance(i.root),
    12.   $emit: i => i.emit,
    13.   $options: i => (__FEATURE_OPTIONS_API__ ? resolveMergedOptions(i) : i.type),
    14.   $forceUpdate: i => () => queueJob(i.update),
    15.   $nextTick: i => nextTick.bind(i.proxy!),
    16.   $watch: i => (__FEATURE_OPTIONS_API__ ? instanceWatch.bind(i) : NOOP)
    17. } as PublicPropertiesMap)

    在 publicPropertiesMap 對(duì)象中,我們找到了 $emit 屬性,該屬性的值為 $emit: i => i.emit,即 $emit 指向的是參數(shù) i 對(duì)象的 emit 屬性。下面我們來(lái)看一下,當(dāng)獲取 $emit 屬性時(shí),target 對(duì)象是什么:

    由上圖可知 target 對(duì)象有一個(gè) _ 屬性,該屬性的值是一個(gè)對(duì)象,且該對(duì)象含有 vnode、type 和 parent 等屬性。因此我們猜測(cè) _ 屬性的值是組件實(shí)例。為了證實(shí)這個(gè)猜測(cè),利用 Chrome 開(kāi)發(fā)者工具,我們就可以輕易地分析出組件掛載過(guò)程中調(diào)用了哪些函數(shù):

    在上圖中,我們看到了在組件掛載階段,調(diào)用了 createComponentInstance 函數(shù)。顧名思義,該函數(shù)用于創(chuàng)建組件實(shí)例,其具體實(shí)現(xiàn)如下所示:

     
     
     
     
    1. // packages/runtime-core/src/component.ts
    2. export function createComponentInstance(
    3.   vnode: VNode,
    4.   parent: ComponentInternalInstance | null,
    5.   suspense: SuspenseBoundary | null
    6. ) {
    7.   const type = vnode.type as ConcreteComponent
    8.   const appContext =
    9.     (parent ? parent.appContext : vnode.appContext) || emptyAppContext
    10.   const instance: ComponentInternalInstance = {
    11.     uid: uid++,
    12.     vnode,
    13.     type,
    14.     parent,
    15.     appContext,
    16.     // 省略大部分屬性
    17.     emit: null as any, 
    18.     emitted: null,
    19.   }
    20.   if (__DEV__) { // 開(kāi)發(fā)模式
    21.     instance.ctx = createRenderContext(instance)
    22.   } else { // 生產(chǎn)模式
    23.     instance.ctx = { _: instance }
    24.   }
    25.   instance.root = parent ? parent.root : instance
    26.   instance.emit = emit.bind(null, instance)
    27.   return instance
    28. }

    在以上代碼中,我們除了發(fā)現(xiàn) instance 對(duì)象之外,還看到了 instance.emit = emit.bind(null, instance) 這個(gè)語(yǔ)句。這時(shí)我們就找到了 $emit 方法來(lái)自哪里的答案。弄清楚第一個(gè)問(wèn)題之后,接下來(lái)我們來(lái)分析自定義事件的處理流程。

    二、自定義事件的處理流程是什么?

    要搞清楚,為什么點(diǎn)擊 歡迎 按鈕派發(fā) welcome 事件之后,就會(huì)自動(dòng)調(diào)用 sayHi 方法的原因。我們就必須分析 emit 函數(shù)的內(nèi)部處理邏輯,該函數(shù)被定義在 runtime-core/src/componentEmits.t 文件中:

     
     
     
     
    1. // packages/runtime-core/src/componentEmits.ts
    2. export function emit(
    3.   instance: ComponentInternalInstance,
    4.   event: string,
    5.   ...rawArgs: any[]
    6. ) {
    7.   const props = instance.vnode.props || EMPTY_OBJ
    8.  // 省略大部分代碼
    9.   let args = rawArgs
    10.   // convert handler name to camelCase. See issue #2249
    11.   let handlerName = toHandlerKey(camelize(event))
    12.   let handler = props[handlerName]
    13.   if (handler) {
    14.     callWithAsyncErrorHandling(
    15.       handler,
    16.       instance,
    17.       ErrorCodes.COMPONENT_EVENT_HANDLER,
    18.       args
    19.     )
    20.   }
    21. }

    其實(shí)在 emit 函數(shù)內(nèi)部還會(huì)涉及 v-model update:xxx 事件的處理,關(guān)于 v-model 指令的內(nèi)部原理,阿寶哥會(huì)寫(xiě)單獨(dú)的文章來(lái)介紹。這里我們只分析與當(dāng)前示例相關(guān)的處理邏輯。

    在 emit 函數(shù)中,會(huì)使用 toHandlerKey 函數(shù)把事件名轉(zhuǎn)換為駝峰式的 handlerName:

     
     
     
     
    1. // packages/shared/src/index.ts
    2. export const toHandlerKey = cacheStringFunction(
    3.   (str: string) => (str ? `on${capitalize(str)}` : ``)
    4. )

    在獲取 handlerName 之后,就會(huì)從 props 對(duì)象上獲取該 handlerName 對(duì)應(yīng)的 handler對(duì)象。如果該 handler 對(duì)象存在,則會(huì)調(diào)用 callWithAsyncErrorHandling 函數(shù),來(lái)執(zhí)行當(dāng)前自定義事件對(duì)應(yīng)的事件處理函數(shù)。callWithAsyncErrorHandling 函數(shù)的定義如下:

     
     
     
     
    1. // packages/runtime-core/src/errorHandling.ts
    2. export function callWithAsyncErrorHandling(
    3.   fn: Function | Function[],
    4.   instance: ComponentInternalInstance | null,
    5.   type: ErrorTypes,
    6.   args?: unknown[]
    7. ): any[] {
    8.   if (isFunction(fn)) {
    9.     const res = callWithErrorHandling(fn, instance, type, args)
    10.     if (res && isPromise(res)) {
    11.       res.catch(err => {
    12.         handleError(err, instance, type)
    13.       })
    14.     }
    15.     return res
    16.   }
    17.   // 處理多個(gè)事件處理器
    18.   const values = []
    19.   for (let i = 0; i < fn.length; i++) {
    20.     values.push(callWithAsyncErrorHandling(fn[i], instance, type, args))
    21.   }
    22.   return values
    23. }

    通過(guò)以上代碼可知,如果 fn 參數(shù)是函數(shù)對(duì)象的話,在 callWithAsyncErrorHandling 函數(shù)內(nèi)部還會(huì)繼續(xù)調(diào)用 callWithErrorHandling 函數(shù)來(lái)最終執(zhí)行事件處理函數(shù):

     
     
     
     
    1. // packages/runtime-core/src/errorHandling.ts
    2. export function callWithErrorHandling(
    3.   fn: Function,
    4.   instance: ComponentInternalInstance | null,
    5.   type: ErrorTypes,
    6.   args?: unknown[]
    7. ) {
    8.   let res
    9.   try {
    10.     res = args ? fn(...args) : fn()
    11.   } catch (err) {
    12.     handleError(err, instance, type)
    13.   }
    14.   return res
    15. }

    在 callWithErrorHandling 函數(shù)內(nèi)部,使用 try catch 語(yǔ)句來(lái)捕獲異常并進(jìn)行異常處理。如果調(diào)用 fn 事件處理函數(shù)之后,返回的是一個(gè) Promise 對(duì)象的話,則會(huì)通過(guò) Promise 對(duì)象上的 catch 方法來(lái)處理異常。了解完上面的內(nèi)容,再回顧一下前面見(jiàn)過(guò)的函數(shù)調(diào)用棧,相信此時(shí)你就不會(huì)再陌生了。

    現(xiàn)在前面提到的 2 個(gè)問(wèn)題,我們都已經(jīng)找到答案了。為了能更好地掌握自定義事件的相關(guān)內(nèi)容,阿寶哥將使用 Vue 3 Template Explorer 這個(gè)在線工具,來(lái)分析一下示例中模板編譯的結(jié)果:

    App 組件模板

     
     
     
     
    1. const _Vue = Vue
    2. return function render(_ctx, _cache, $props, $setup, $data, $options) {
    3.   with (_ctx) {
    4.     const { resolveComponent: _resolveComponent, createVNode: _createVNode, 
    5.       openBlock: _openBlock, createBlock: _createBlock } = _Vue
    6.     const _component_welcome_button = _resolveComponent("welcome-button")
    7.     return (_openBlock(), _createBlock(_component_welcome_button,
    8.      { onWelcome: sayHi }, null, 8 /* PROPS */, ["onWelcome"]))
    9.   }
    10. }

    welcome-button 組件模板

     
     
     
     
    1. 歡迎
    2. const _Vue = Vue
    3. return function render(_ctx, _cache, $props, $setup, $data, $options) {
    4.   with (_ctx) {
    5.     const { createVNode: _createVNode, openBlock: _openBlock,
    6.       createBlock: _createBlock } = _Vue
    7.     return (_openBlock(), _createBlock("button", {
    8.       onClick: $event => ($emit('welcome'))
    9.     }, "歡迎", 8 /* PROPS */, ["onClick"]))
    10.   }
    11. }

    觀察以上結(jié)果,我們可知通過(guò) v-on: 綁定的事件,都會(huì)轉(zhuǎn)換為以 on 開(kāi)頭的屬性,比如 onWelcome 和 onClick。為什么要轉(zhuǎn)換成這種形式呢?這是因?yàn)樵?emit 函數(shù)內(nèi)部會(huì)通過(guò) toHandlerKey 和 camelize 這兩個(gè)函數(shù)對(duì)事件名進(jìn)行轉(zhuǎn)換:

     
     
     
     
    1. // packages/runtime-core/src/componentEmits.ts
    2. export function emit(
    3.   instance: ComponentInternalInstance,
    4.   event: string,
    5.   ...rawArgs: any[]
    6. ) {
    7.  // 省略大部分代碼
    8.   // convert handler name to camelCase. See issue #2249
    9.   let handlerName = toHandlerKey(camelize(event))
    10.   let handler = props[handlerName]
    11. }

    為了搞清楚轉(zhuǎn)換規(guī)則,我們先來(lái)看一下 camelize 函數(shù):

     
     
     
     
    1. // packages/shared/src/index.ts
    2. const camelizeRE = /-(\w)/g
    3. export const camelize = cacheStringFunction(
    4.   (str: string): string => {
    5.     return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
    6.   }
    7. )

    觀察以上代碼,我們可以知道 camelize 函數(shù)的作用,用于把 kebab-case (短橫線分隔命名) 命名的事件名轉(zhuǎn)換為 camelCase (駝峰命名法) 的事件名,比如 "test-event" 事件名經(jīng)過(guò) camelize 函數(shù)處理后,將被轉(zhuǎn)換為 "testEvent"。該轉(zhuǎn)換后的結(jié)果,還會(huì)通過(guò) toHandlerKey 函數(shù)進(jìn)行進(jìn)一步處理,toHandlerKey 函數(shù)被定義在 shared/src/index.ts文件中:

     
     
     
     
    1. // packages/shared/src/index.ts
    2. export const toHandlerKey = cacheStringFunction(
    3.   (str: string) => (str ? `on${capitalize(str)}` : ``)
    4. )
    5. export const capitalize = cacheStringFunction(
    6.   (str: string) => str.charAt(0).toUpperCase() + str.slice(1)
    7. )

    對(duì)于前面使用的 "testEvent" 事件名經(jīng)過(guò) toHandlerKey 函數(shù)處理后,將被最終轉(zhuǎn)換為 "onTestEvent" 的形式。為了能夠更直觀地了解事件監(jiān)聽(tīng)器的合法形式,我們來(lái)看一下 runtime-core 模塊中的測(cè)試用例:

     
     
     
     
    1. // packages/runtime-core/__tests__/componentEmits.spec.ts
    2. test('isEmitListener', () => {
    3.   const options = {
    4.     click: null,
    5.     'test-event': null,
    6.     fooBar: null,
    7.     FooBaz: null
    8.   }
    9.   expect(isEmitListener(options, 'onClick')).toBe(true)
    10.   expect(isEmitListener(options, 'onclick')).toBe(false)
    11.   expect(isEmitListener(options, 'onBlick')).toBe(false)
    12.   // .once listeners
    13.   expect(isEmitListener(options, 'onClickOnce')).toBe(true)
    14.   expect(isEmitListener(options, 'onclickOnce')).toBe(false)
    15.   // kebab-case option
    16.   expect(isEmitListener(options, 'onTestEvent')).toBe(true)
    17.   // camelCase option
    18.   expect(isEmitListener(options, 'onFooBar')).toBe(true)
    19.   // PascalCase option
    20.   expect(isEmitListener(options, 'onFooBaz')).toBe(true)
    21. })

    了解完事件監(jiān)聽(tīng)器的合法形式之后,我們?cè)賮?lái)看一下 cacheStringFunction 函數(shù):

     
     
     
     
    1. // packages/shared/src/index.ts
    2. const cacheStringFunction =  string>(fn: T): T => {
    3.   const cache: Record = Object.create(null)
    4.   return ((str: string) => {
    5.     const hit = cache[str]
    6.     return hit || (cache[str] = fn(str))
    7.   }) as any
    8. }

    以上代碼也比較簡(jiǎn)單,cacheStringFunction 函數(shù)的作用是為了實(shí)現(xiàn)緩存功能。

    三、阿寶哥有話說(shuō)

    3.1 如何在渲染函數(shù)中綁定事件?

    在前面的示例中,我們通過(guò) v-on 指令完成事件綁定,那么在渲染函數(shù)中如何綁定事件呢?

     
     
     
     
  • 在以上示例中,我們通過(guò) defineComponent 全局 API 定義了 Foo 組件,然后通過(guò) h 函數(shù)創(chuàng)建了函數(shù)式組件 Comp,在創(chuàng)建 Comp 組件時(shí),通過(guò)設(shè)置 onFoo 屬性實(shí)現(xiàn)了自定義事件的綁定操作。

    3.2 如何只執(zhí)行一次事件處理器?

    在模板中設(shè)置

     
     
     
     
    1. const _Vue = Vue
    2. return function render(_ctx, _cache, $props, $setup, $data, $options) {
    3.   with (_ctx) {
    4.     const { resolveComponent: _resolveComponent, createVNode: _createVNode, 
    5.       openBlock: _openBlock, createBlock: _createBlock } = _Vue
    6.     const _component_welcome_button = _resolveComponent("welcome-button")
    7.     return (_openBlock(), _createBlock(_component_welcome_button, 
    8.       { onWelcomeOnce: sayHi }, null, 8 /* PROPS */, ["onWelcomeOnce"]))
    9.   }
    10. }

    在以上代碼中,我們使用了 once 事件修飾符,來(lái)實(shí)現(xiàn)只執(zhí)行一次事件處理器的功能。除了 once 修飾符之外,還有其他的修飾符,比如:

     
     
     
     
    1. ...
  • ...
  •  在渲染函數(shù)中設(shè)置

     
     
     
     
  • 以上兩種方式都能生效的原因是,模板中的指令 v-on:welcome.once,經(jīng)過(guò)編譯后會(huì)轉(zhuǎn)換為onWelcomeOnce,并且在 emit 函數(shù)中定義了 once 修飾符的處理規(guī)則:

     
     
     
     
    1. // packages/runtime-core/src/componentEmits.ts
    2. export function emit(
    3.   instance: ComponentInternalInstance,
    4.   event: string,
    5.   ...rawArgs: any[]
    6. ) {
    7.   const props = instance.vnode.props || EMPTY_OBJ
    8.   const onceHandler = props[handlerName + `Once`]
    9.   if (onceHandler) {
    10.     if (!instance.emitted) {
    11.       ;(instance.emitted = {} as Record)[handlerName] = true
    12.     } else if (instance.emitted[handlerName]) {
    13.       return
    14.     }
    15.     callWithAsyncErrorHandling(
    16.       onceHandler,
    17.       instance,
    18.       ErrorCodes.COMPONENT_EVENT_HANDLER,
    19.       args
    20.     )
    21.   }
    22. }

    3.3 如何添加多個(gè)事件處理器

    在模板中設(shè)置

      
     
     
     
    1.   
    2. const _Vue = Vue
    3. return function render(_ctx, _cache, $props, $setup, $data, $options) {
    4.   with (_ctx) {
    5.     const { createVNode: _createVNode, openBlock: _openBlock, 
    6.       createBlock: _createBlock } = _Vue
    7.     return (_openBlock(), _createBlock("div", {
    8.       onClick: $event => (foo(), bar())
    9.     }, null, 8 /* PROPS */, ["onClick"]))
    10.   }
    11. }

     在渲染函數(shù)中設(shè)置

      
     
     
     
  • 以上方式能夠生效的原因是,在前面介紹的 callWithAsyncErrorHandling 函數(shù)中含有多個(gè)事件處理器的處理邏輯:

      
     
     
     
    1. // packages/runtime-core/src/errorHandling.ts
    2. export function callWithAsyncErrorHandling(
    3.   fn: Function | Function[],
    4.   instance: ComponentInternalInstance | null,
    5.   type: ErrorTypes,
    6.   args?: unknown[]
    7. ): any[] {
    8.   if (isFunction(fn)) {
    9.    // 省略部分代碼
    10.   }
    11.   const values = []
    12.   for (let i = 0; i < fn.length; i++) {
    13.     values.push(callWithAsyncErrorHandling(fn[i], instance, type, args))
    14.   }
    15.   return values
    16. }

    3.4 Vue 3 的 $emit 與 Vue 2 的 $emit 有什么區(qū)別?

    在 Vue 2 中 $emit 方法是 Vue.prototype 對(duì)象上的屬性,而 Vue 3 上的 $emit 是組件實(shí)例上的一個(gè)屬性,instance.emit = emit.bind(null, instance)。

      
     
     
     
    1. // src/core/instance/events.js
    2. export function eventsMixin (Vue: Class) {
    3.   const hookRE = /^hook:/
    4.   // 省略$on、$once和$off等方法的定義
    5.   // Vue實(shí)例是一個(gè)EventBus對(duì)象
    6.   Vue.prototype.$emit = function (event: string): Component {
    7.     const vm: Component = this
    8.     let cbs = vm._events[event]
    9.     if (cbs) {
    10.       cbs = cbs.length > 1 ? toArray(cbs) : cbs
    11.       const args = toArray(arguments, 1)
    12.       const info = `event handler for "${event}"`
    13.       for (let i = 0, l = cbs.length; i < l; i++) {
    14.         invokeWithErrorHandling(cbs[i], vm, args, vm, info)
    15.       }
    16.     }
    17.     return vm
    18.   }
    19. }

    本文阿寶哥主要介紹了在 Vue 3 中自定義事件背后的秘密。為了讓大家能夠更深入地掌握自定義事件的相關(guān)知識(shí),阿寶哥從源碼的角度分析了 $emit 方法的來(lái)源和自定義事件的處理流程。

    在 Vue 3.0 進(jìn)階系列第一篇文章 Vue 3.0 進(jìn)階之指令探秘 中,我們已經(jīng)介紹了指令相關(guān)的知識(shí),有了這些基礎(chǔ),之后阿寶哥將帶大家一起探索 Vue 3 雙向綁定的原理,感興趣的小伙伴不要錯(cuò)過(guò)喲。

    四、參考資源

    • Vue 3 官網(wǎng) - 事件處理
    • Vue 3 官網(wǎng) - 自定義事件
    • Vue 3 官網(wǎng) - 全局 API

    網(wǎng)頁(yè)標(biāo)題:Vue3.0進(jìn)階之自定義事件探秘
    文章URL:http://m.5511xx.com/article/ccoooch.html