This article will talk about the virtual DOM in vue, introduce what virtual dom is, the benefits of introducing vdom, and why vue uses vdom. I hope it will be helpful to everyone!
Almost all existing frameworks introduce virtual DOM to abstract the real DOM, which is now known as VNode and VDOM. So why do we need to introduce virtual DOM? ? This article will answer this question! (Learning video sharing: vuejs introductory tutorial)
Virtual domAs the name suggests, it is a virtual dom object, itself It is a JavaScript object, but it describes a view structure through different attributes.
1. Abstract real element nodes into VNode, effectively reducing the number of direct operations on dom, thereby improving program performance
2. To facilitate cross-platform implementation
SSR(Nuxt.js/Next.js)
and native application (Weex/React Native
), small programs (mpvue/uni-app
), etc., rendering into WebGL, etc. Additional: Why does vue use vdom?
Introducing Virtual DOM
Performance considerations are only one aspect.
Performance is greatly affected by scenarios. Different scenarios may cause exponential performance gaps between different implementation solutions, so rely on fine-grained binding and Virtual DOM
Which performance Better is really not an easy question to draw a conclusion on.
Vue
The more important reason why Virtual DOM
was introduced is to decouple HTML
dependencies. This It brings two very important benefits:
HTML
parser for template parsing, and can perform more AOT
work to improve operation Time efficiency: By compiling the template AOT
, the runtime volume of Vue
can be further compressed, and the runtime efficiency can be further improved; DOM# Platforms other than ## implement advanced features such as
SSR and isomorphic rendering. Frameworks such as
Weex apply this feature.
To sum up,3. How vdom is generated and how it becomes a real domIn vue we often write templates for components, this template will be compiled by the compiler for rendering function, the render function will be called during the subsequent mount process, and the returned object is the virtual dom. But they are not real DOM yet, so they will be further converted into DOM in the subsequent patch process. 4. Role in subsequent diffAfter the mounting process is completed, the vue program enters the update process. If some responsive data changes, it will cause the component to be re-rendered. At this time, a new vdom will be generated, and the changes will be obtained by diffing the last rendering result, thus converting it into a minimum amount of DOM operations and efficient updates. view. Direct source codevnode definition: https://github1s.com/vuejs/core/blob/HEAD/packages/runtime-core/src/vnode.ts#L127-L128Virtual DOM
’s performance gains are not the most important. What’s more important is that it makes
VueIt has advanced features that modern frameworks should have.
[Create vnode]:
createElementBlock:https://github1s.com/vuejs/core/blob/HEAD/packages/runtime-core/src/vnode.ts #L291-L292createVnode:https://github1s.com/vuejs/core/blob/HEAD/packages/runtime-core/src/vnode.ts#L486-L487First call time: https://github1s.com/vuejs/core/blob/HEAD/packages/runtime-core/src/apiCreateApp.ts#L283-L284
mount: https:// github1s.com/vuejs/core/blob/HEAD/packages/runtime-core/src/renderer.ts#L1171-L1172
(Learning video sharing: web front-end)
The above is the detailed content of Let's talk about what is virtual dom? Why does vue use vdom?. For more information, please follow other related articles on the PHP Chinese website!