Abstract: The concept of the new version of The concept of the new version of Vue3 was formed at the end of 2018, when Vue 2 was already two and a half years old. Compared with the life cycle of general software, this doesn’t seem that long. Vue3 was officially launched in 2020, with major changes in source code and API. The performance has been significantly improved, which is 1.2~2 times faster than Vue2.x. Among them, some of the more important advantages are: Optimization of diff algorithm: Let's talk about the advantages of vue3 in detail: Advantage 1: Optimization of diff algorithmThe virtual DOM in Vue2 is a full comparison (each node, whether hardcoded or dynamic, will be compared layer by layer, which wastes most of the events on comparing static nodes) Vue3 adds a static flag ( For example, the following template contains a div with three paragraphs. The first two paragraphs are static and fixed, while the content of the third paragraph is bound to the <div> <p>Cloud Residence Co-creation</p> <p>How to evaluate vue3</p> <p>{{msg}}</p> </div> When the view is updated, only the dynamic nodes are diffed, which reduces resource consumption. Advantage 2: hoistStatic static lifting Vue2 will recreate and render each time regardless of whether the element participates in the update. For elements that do not participate in the update, Vue3 will perform static promotion. They will only be created once and can be reused directly during rendering. For example, let's use <div> <div>Co-create 1</div> <div>Co-create 2</div> <div>{{name}}</div> </div> Before static lifting export function render(...) { return ( _openBlock(), _createBlock('div', null, [ _createVNode('div', null, 'Co-create 1'), _createVNode('div', null, 'Co-creation 2'), _createVNode( 'div', null, _toDisplayString(_ctx.name), 1 /* TEXT */ ), ]) ) } After static lifting const _hoisted_1 = /*#__PURE__*/ _createVNode( 'div', null, 'Co-create 1', -1 /* HOISTED */ ) const _hoisted_2 = /*#__PURE__*/ _createVNode( 'div', null, 'Co-creation 2', -1 /* HOISTED */ ) export function render(...) { return ( _openBlock(), _createBlock('div', null, [ _hoisted_1, _hoisted_2, _createVNode( 'div', null, _toDisplayString(_ctx.name), 1 /* TEXT */ ), ]) ) } From the above code Advantage 3: cacheHandlers event listener cache In vue2.x, a new By default, For example, let's use <div> <div @click="todo">Do something fun</div> </div> After compilation, this section of export function render(...) { return (_openBlock(),_createBlock('div', null, [ _createVNode('div',{ onClick: _ctx.todo}, 'Do something interesting', 8 /* PROPS */, ['onClick']), ]) ) } When we turn on event listener caching: export function render(...) { return (_openBlock(),_createBlock('div', null, [ _createVNode('div',{ onClick: //After enabling monitoring_cache[1] || (_cache[1] = (...args) =>_ctx.todo(...args)), },'Do something interesting'), ]) ) } We can compare the code before and after turning on the event listening cache. You may not understand the code after the conversion, but it doesn’t matter. We just need to observe whether there are static tags. In the diff algorithm of Vue3, only those with static tags will be compared and tracked. Advantage 4: SSR renderingVue2 also has SSR rendering, but the SSR rendering in Vue3 has corresponding performance improvements compared to Vue2. When there is a large amount of static content, this content will be pushed into a When the static content is large enough, the Advantage 5: Better Ts support Vue2 is not suitable for using ts because of the In the specific practice of combining vue2 with ts, In vue3, a import { defineComponent, ref } from 'vue' const Component = defineComponent({ props: { success: { type: String }, student: type: Object as PropType<Student>, required: true } }, setup() { const year = ref(2020) const month = ref<string | number>('9') month.value = 9 // OK const result = year.value.split('') } Advantage 6: Compostion API: Combination API/Injection API Traditional web pages are separated by The In vue3, use the import useCounter from './counter' import useTodo from './todos' setup(){ let { val, todos, addTodo } = useTodo() let { count, add } = useCounter() return { val, todos, addTodo, count,add, } Advantage 7: More advanced components Vue2 does not allow this to be written. The component must have a root node. Now you can write it like this, and Vue will create a virtual <template> <div>Huawei Cloud Experts</div> <div>Full stack blogger</div> </template> Before Advantage 8: Custom Rendering API The vue2.x project architecture is not very friendly for rendering to different platforms such as weex (a cross-platform solution for mobile devices) and vue2: import Vue from 'vue' import App from './App.vue' new Vue({ => h(App)}).$mount('#app') vue3: const { createApp } from 'vue' import App from "./src/App" createApp(App).mount(('#app')
import { createApp } from "./runtime-render"; import App from "./src/App"; // Root component createApp(App).mount('#app'); Using custom rendering APIs, such as Advantage 9: Compile on demand, smaller in size than vue2.xThe size of the frame also affects its performance. This is a unique concern for web applications, because since resources need to be downloaded on the fly, the application is not interactive until the browser has parsed the necessary JavaScript. This is especially true for single-page applications. Although Vue has always been relatively lightweight (Vue 2's runtime size is 23 KB compressed). In Vue 3, this was achieved by moving most of the global API and internal helpers to ES module exports. This allows modern bundlers to statically analyze module dependencies and remove unused export-related code. The template compiler also generates friendly Some parts of the framework will never be Advantage 10: Support multiple root node components In Vue3, a template is no longer limited to multiple root nodes. ( In Vue 3, components now officially support multi-root components, aka fragments! In 2.x, multi-root components were not supported and a warning was issued when the user accidentally created a multi-root component, so to fix this bug many components were wrapped in one. as follows <template> <div> <header>...</header> <main>...</main> <footer>...</footer> </div> </template> In 3.x, components can now have multiple root nodes! However, this does require the developer to explicitly define where the attributes should be distributed. <template> <header>...</header> <main v-bind="$attrs">...</main> <footer>...</footer> </template> Summarize:
This is the end of this article about the advantages of vue3 over vue2. For more information about the advantages of vue3 over vue2, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Teach you how to insert 1 million records into MySQL in 6 seconds
>>: Let's talk about the storage engine in MySQL
Table of contents Environmental conditions Errors...
Table of contents What is ReactHook? React curren...
For Linux system administrators, it is crucial to...
Table of contents Preface Option 1: Option 2: Opt...
This article uses examples to illustrate the usag...
Basics In a relational database, each data table ...
The detailed process of using MySQL database with...
The goal of this document is to explain the Json ...
I’ve always preferred grayscale images because I t...
background I originally wanted to download a 6.7 ...
Although Microsoft provides T4 templates, I find ...
Table of contents The browser's rendering mec...
What is the role of http in node The responsibili...
<br />It has been no more than two years sin...
Overview In the previous chapter, we learned abou...