国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

讓ChatGPT解讀Vue3源碼過(guò)程解析

瀏覽:54日期:2022-06-01 18:25:52
目錄
  • 前言
  • 實(shí)戰(zhàn)
    • setup
    • 小結(jié)
  • 總結(jié)

    前言

    ChatGPT 最近十分火爆,今天我也來(lái)讓 ChatGPT 幫我閱讀一下 Vue3 的源代碼。

    都知道 Vue3 組件有一個(gè) setup函數(shù)。那么它內(nèi)部做了什么呢,今天跟隨 ChatGPT 來(lái)一探究竟。

    實(shí)戰(zhàn)

    setup

    setup 函數(shù)在什么位置呢,我們不知道他的實(shí)現(xiàn)函數(shù)名稱,于是問(wèn)一下 ChatGPT:

    ChatGPT 告訴我,setup 函數(shù)在packages/runtime-core/src/component.ts 文件中。眾所周知,runtime-core是 Vue3 的運(yùn)行時(shí)核心代碼。我們進(jìn)去看一眼。

    按照它所說(shuō)的,我們找到了 setupComponentcreateComponentInstance 函數(shù),并沒(méi)有找到 setupRenderEffect 函數(shù),ChatGPT 的只知道 2021 年以前的知識(shí),Vue3 代碼經(jīng)過(guò)了很多變動(dòng),不過(guò)沒(méi)關(guān)系,這不影響太多。

    ChatGPT 告訴我,setupComponent 函數(shù)是在createComponentInstance函數(shù)中執(zhí)行的,createComponentInstance看名字是創(chuàng)建組件實(shí)例,看一下詳細(xì)代碼。

    直接復(fù)制給 ChatGPT:

    我們根據(jù) ChatGPT 的解釋來(lái)閱讀代碼,發(fā)現(xiàn)createComponentInstance只是創(chuàng)建了組件的實(shí)例并返回。并沒(méi)有像它上面說(shuō)的在函數(shù)中執(zhí)行了 setupComponent,笨笨的 ChatGPT。

    那就自己找一下setupComponent是在哪里被調(diào)用的。

    可以packages/runtime-core/搜一下函數(shù)名,很快就找到了。在packages/runtime-core/src/renderer.ts文件中的mountComponent函數(shù)中。

    mountComponent 是掛載組件的方法,前面還有一堆自定義渲染器的邏輯,不在此篇展開。

      const mountComponent: MountComponentFn = (...args) => {    const instance: ComponentInternalInstance =      compatMountInstance ||      (initialVNode.component = createComponentInstance(initialVNode,parentComponent,parentSuspense      ))    // ... 省略代碼    // resolve props and slots for setup context    if (!(__COMPAT__ && compatMountInstance)) {// ...這里調(diào)用了setupComponent,傳入了實(shí)例,還寫了注釋,感人      setupComponent(instance)    }    // setupRenderEffect 居然也在這    setupRenderEffect(      instance,      initialVNode,      container,      anchor,      parentSuspense,      isSVG,      optimized    )  }

    mountComponent函數(shù)先調(diào)用了createComponentInstance, 返回個(gè)組件實(shí)例,又把實(shí)例當(dāng)作參數(shù)傳給了 setupComponent。順便我們還在這發(fā)現(xiàn)了 ChatGPT 搞丟的setupRenderEffect函數(shù),它是用來(lái)處理一些渲染副作用的。

    回到 setupComponent函數(shù),Evan 的注釋告訴我們它是處理 props 和 slots 的。

    export function setupComponent(  instance: ComponentInternalInstance,  isSSR = false) {  isInSSRComponentSetup = isSSR  const { props, children } = instance.vnode  const isStateful = isStatefulComponent(instance)  initProps(instance, props, isStateful, isSSR)  initSlots(instance, children)  const setupResult = isStateful    ? setupStatefulComponent(instance, isSSR)    : undefined  isInSSRComponentSetup = false  return setupResult}

    把代碼喂給 ChatGPT:

    setupComponent 函數(shù)中,處理完 props 和 slots 后,根據(jù)是否是有狀態(tài)組件調(diào)用了setupStatefulComponent。

    直接整個(gè) setupStatefulComponent喂給 ChatGPT:

    太長(zhǎng)了,大概意思:

    • 創(chuàng)建了代理緩存accessCache,干嘛用的咱也不知道,可以問(wèn) ChatGPT
    • 創(chuàng)建公共實(shí)例代理對(duì)象(proxy)
    • 執(zhí)行組件的 setup()

    后續(xù)操作是調(diào)用 handleSetupResultfinishComponentSetup 返回渲染函數(shù)。開始走渲染邏輯了。

    小結(jié)

    小結(jié)一下setup的始末:

    • 從組件掛載開始調(diào)用createComponentInstance創(chuàng)建組件實(shí)例
    • 傳遞組件實(shí)例給setupComponent
    • setupComponent內(nèi)部初始化 props 和 slots
    • setupStatefulComponent 執(zhí)行組件的setup
    • 完成 setup 流程
    • 返回渲染函數(shù)
    • ...

    總結(jié)

    ChatGPT 很強(qiáng)大,也很笨,畢竟它不聯(lián)網(wǎng),且只有 2021 年以前的數(shù)據(jù)??捎脕?lái)幫助我們讀一下晦澀的源碼還是可以的,但也只能輔助作用,還需要自己的思考。

    以上就是讓ChatGPT解讀Vue3源碼過(guò)程解析的詳細(xì)內(nèi)容,更多關(guān)于ChatGPT讀Vue3源碼的資料請(qǐng)關(guān)注其它相關(guān)文章!

    標(biāo)簽: JavaScript
    主站蜘蛛池模板: 安岳县| 利辛县| 信阳市| 建湖县| 涿州市| 如皋市| 宾阳县| 应用必备| 西畴县| 府谷县| 许昌县| 阳曲县| 秭归县| 满洲里市| 深圳市| 和静县| 闽清县| 彭山县| 兴海县| 鄂伦春自治旗| 齐河县| 云安县| 克什克腾旗| 上思县| 光山县| 青浦区| 东台市| 会理县| 宁陵县| 重庆市| 庆云县| 新乐市| 湖南省| 逊克县| 兴隆县| 揭西县| 彭水| 衡山县| 昌图县| 闵行区| 建始县|