讓ChatGPT解讀Vue3源碼過程解析
前言
ChatGPT 最近十分火爆,今天我也來讓 ChatGPT 幫我閱讀一下 Vue3 的源代碼。
都知道 Vue3 組件有一個(gè) setup
函數(shù)。那么它內(nèi)部做了什么呢,今天跟隨 ChatGPT 來一探究竟。
實(shí)戰(zhàn)
setup
setup
函數(shù)在什么位置呢,我們不知道他的實(shí)現(xiàn)函數(shù)名稱,于是問一下 ChatGPT:
ChatGPT 告訴我,setup
函數(shù)在packages/runtime-core/src/component.ts
文件中。眾所周知,runtime-core
是 Vue3 的運(yùn)行時(shí)核心代碼。我們進(jìn)去看一眼。
按照它所說的,我們找到了 setupComponent
和 createComponentInstance
函數(shù),并沒有找到 setupRenderEffect
函數(shù),ChatGPT 的只知道 2021 年以前的知識(shí),Vue3 代碼經(jīng)過了很多變動(dòng),不過沒關(guān)系,這不影響太多。
ChatGPT 告訴我,setupComponent
函數(shù)是在createComponentInstance
函數(shù)中執(zhí)行的,createComponentInstance
看名字是創(chuàng)建組件實(shí)例,看一下詳細(xì)代碼。
直接復(fù)制給 ChatGPT:
我們根據(jù) ChatGPT 的解釋來閱讀代碼,發(fā)現(xiàn)createComponentInstance
只是創(chuàng)建了組件的實(shí)例并返回。并沒有像它上面說的在函數(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ù),它是用來處理一些渲染副作用的。
回到 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:
太長了,大概意思:
- 創(chuàng)建了代理緩存accessCache,干嘛用的咱也不知道,可以問 ChatGPT
- 創(chuàng)建公共實(shí)例代理對(duì)象(proxy)
- 執(zhí)行組件的 setup()
后續(xù)操作是調(diào)用 handleSetupResult
和 finishComponentSetup
返回渲染函數(shù)。開始走渲染邏輯了。
小結(jié)
小結(jié)一下setup
的始末:
- 從組件掛載開始調(diào)用
createComponentInstance
創(chuàng)建組件實(shí)例 - 傳遞組件實(shí)例給
setupComponent
setupComponent
內(nèi)部初始化 props 和 slotssetupStatefulComponent
執(zhí)行組件的setup
- 完成 setup 流程
- 返回渲染函數(shù)
- ...
總結(jié)
ChatGPT 很強(qiáng)大,也很笨,畢竟它不聯(lián)網(wǎng),且只有 2021 年以前的數(shù)據(jù)??捎脕韼椭覀冏x一下晦澀的源碼還是可以的,但也只能輔助作用,還需要自己的思考。
以上就是讓ChatGPT解讀Vue3源碼過程解析的詳細(xì)內(nèi)容,更多關(guān)于ChatGPT讀Vue3源碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue-element內(nèi)table插入超鏈接a標(biāo)簽的使用
在Vue Element的table組件中插入超鏈接,可以使用<el-link>標(biāo)簽替代傳統(tǒng)的<a>標(biāo)簽,實(shí)現(xiàn)更加整潔的UI設(shè)計(jì),具體操作是替換原有的<span>標(biāo)簽,直接使用<el-link>進(jìn)行超鏈接的插入,使得鏈接樣式與Element UI保持一致2024-09-09關(guān)于element-ui?單選框默認(rèn)值不選中的解決
這篇文章主要介紹了關(guān)于element-ui?單選框默認(rèn)值不選中的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue3.x使用swiper實(shí)現(xiàn)卡片輪播
這篇文章主要為大家詳細(xì)介紹了vue3.x使用swiper實(shí)現(xiàn)卡片輪播,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07