使用非同步元件的原因:1、非同步元件可以減少打包的結果,會將非同步元件分開打包,會採用非同步的方式載入元件,可以有效的解決一個元件過大的問題。 2.非同步元件的核心可以給元件定義變成函數,函數裡面可以用import語法,實作檔案的分割載入。
本教學操作環境:windows7系統、vue3版,DELL G3電腦。
使用非同步元件的原因
1.非同步元件可以減少打包的結果。會將非同步元件分開打包,會採用非同步的方式載入元件,可以有效的解決一個元件過大的問題。不使用非同步組件,如果組件功能比較多打包出來的結果就會變大。
2.非同步元件的核心可以給元件定義變成函數,函數裡面可以用import語法,實作檔案的分割載入,import語法是webpack提供的,採用的就是jsonp。 (學習影片分享:vuejs入門教學、程式設計基礎影片)
components:{ VideoPlay:(resolve)=>import("../components/VideoPlay") } components:{ VideoPlay(resolve) { require(["../components/VideoPlay"], resolve) } } 或者使用回调函数
原理
在createComponent方法中,會有對應的非同步元件處理,先定義一個asyncFactory變量,然後進行判斷,如果組件是一個函數,然後會去調resolveAsyncComponent方法,然後將賦值在asyncFactory上的函數傳進去,會讓asyncFactory馬上執行,執行的時候並不會馬上返回結果,因為他是異步的,回傳的是一個promise,這時候這個值就是undefined,然後就會先渲染一個非同步元件的佔位,空虛擬節點。如果載入完之後會調factory函數傳入resolve和reject兩個參數,執行後傳回一個成功的回呼與失敗的回呼,promise成功了就會調resolve,resolve中就會調取forceRender方法強制更新視圖重新渲染,forceRender中調取的就是$forceUpdate,同時把結果放到factory.resolved上,如果強制刷新的時候就會再次走resolveAsyncComponent方法,這時候有個判斷,如果有成功的結果就把結果直接放回去,這時候resolveAsyncComponent回傳的就不是undefined了,就會接的建立元件,初始化元件,渲染元件。
原始碼
src/core/vdom/create-component.js
1.createComponent方法
export function createComponent ( Ctor: Class<Component> | Function | Object | void, data: ?VNodeData, context: Component, children: ?Array<VNode>, tag?: string ): VNode | Array<VNode> | void { let asyncFactory if (isUndef(Ctor.cid)) { // 看组件是否是一个函数 asyncFactory = Ctor // 异步组件一定是一个函数 新版本提供了对象的写法 Ctor = resolveAsyncComponent(asyncFactory, baseCtor) //默认调用此函数时返回undefiend // 第二次渲染时Ctor不为undefined if (Ctor === undefined) { //返回async组件的占位符节点 //作为注释节点,但保留该节点的所有原始信息 //该信息将用于异步服务器渲染和水合。 return createAsyncPlaceholder( asyncFactory, data, context, children, tag ) } } }
2.resolveAsyncComponent方法
export function resolveAsyncComponent ( factory: Function, baseCtor: Class<component> ): Class<component> | void { // 如果有错误就返回错误结果 if (isTrue(factory.error) && isDef(factory.errorComp)) { return factory.errorComp } // 再次渲染时可以拿到获取的最新组件 // 如果有成功的结果,就直接返回去 if (isDef(factory.resolved)) { return factory.resolved } if (owner && !isDef(factory.owners)) { // forceRender 强制刷新渲染 const forceRender = (renderCompleted: boolean) => { for (let i = 0, l = owners.length; i ) => { factory.resolved = ensureCtor(res, baseCtor) if (!sync) { forceRender(true) // 执行强制更新视图重新渲染方法 } else { owners.length = 0 } }) // 失败 const reject = once(reason => { if (isDef(factory.errorComp)) { factory.error = true forceRender(true) } }) // 执行factory 将resolve方法和reject方法传入 const res = factory(resolve, reject) sync = false return factory.loading ? factory.loadingComp : factory.resolved // 返回结果 } }</component></component>
3.createAsyncPlaceholder 方法
// 创建一个异步组件的占位,空虚拟节点 也就是一个注释
以上是vue為啥要使用非同步組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互動,可及可訪問。 1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒體綜合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityActibility.2)and tagsallowsemlessallowseamelesseamlessallowseamelesseamlesseamelesseamemelessmultimedimeDiaiaembediiaembedplugins.3)。 3)3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrenerabilnerabilityLikeClickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID選擇器在CSS中並非固有地不好,但應謹慎使用。 1)ID選擇器適用於唯一元素或JavaScript鉤子。 2)對於一般樣式,應使用類選擇器,因為它們更靈活和可維護。通過平衡ID和類的使用,可以實現更robust和efficient的CSS架構。

html5'sgoalsin2024focusonrefinement和optimization,notNewFeatures.1)增強performanceandeffipedroptimizedRendering.2)inviveAccessibilitywithRefinedwithRefinedTributesAndEllements.3)explityconcerns,尤其是withercercern.4.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供應,2)語義結構,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,簡化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)