在vue3中,异步组件可以减少打包的结果,会将异步组件分开打包,会采用异步的方式加载组件,可以有效的解决一个组件过大的问题;不使用异步组件,如果组件功能比较多打包出来的结果就会变大。
本教程操作环境:windows7系统、vue2.9.6版,DELL G3电脑。
异步组件是 vue 的一种新能优化的方法,比如可以运用在首屏加载等等场景。
1、异步组件可以减少打包的结果。会将异步组件分开打包,会采用异步的方式加载组件,可以有效的解决一个组件过大的问题。不使用异步组件,如果组件功能比较多打包出来的结果就会变大。
2、异步组件的核心可以给组件定义变成一个函数,函数里面可以用import语法,实现文件的分割加载,import语法是webpack提供的,采用的就是jsonp。
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 < l; i++) { (owners[i]: any).$forceUpdate() // 执行$forceUpdate } } // 成功 const resolve = once((res: Object | Class<Component>) => { 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 // 返回结果 } }
3、createAsyncPlaceholder 方法
// 创建一个异步组件的占位,空虚拟节点 也就是一个注释<!--> export function createAsyncPlaceholder ( factory: Function, data: ?VNodeData, context: Component, children: ?Array<VNode>, tag: ?string ): VNode { const node = createEmptyVNode() node.asyncFactory = factory node.asyncMeta = { data, context, children, tag } return node }
【相关推荐:《vue.js教程》】
以上是vue3异步组件有什么用的详细内容。更多信息请关注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 ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrevulnerabilityLikeClickLickLickLickLickLickjAckJackJacking.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)增强performandemandeffifice throughOptimizedRendering.2)risteccessibilitywithrefinedibilitywithRefineDatientAttributesAndEllements.3)expliencernsandelements.3)explastsecurityConcerns,尤其是withercervion.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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

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

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!