search
HomeWeb Front-endVue.jsThe interviewer suddenly asked: What is the difference between Vue2 and Vue3?

Preface

Nowadays, there are crazy rumors on the Internet that the front-end is dead. In fact, the essential reason is that personnel have increased and positions have decreased, which has led to increased competitive pressure.

We have no way to solve the existing problems in society, but we can start from ourselves to increase our competitiveness.

When multiple people compete for a position, there is actually no difference between the second place and the last place, because they only want the first place.

So it is very important to answer every question to 100 points!

This is also the reason why I wrote "One Question Confuses the Interviewer".

This topic hopes to help everyone get 100 points in each question and "conquer" the interviewer in the shortest time. Cherish every interview opportunity in your hand, and wish you all to join your favorite company with high salary as soon as possible

How should you answer when asked "The difference between vue2 and vue3"

Vue Internally based on functions Can be divided into three large modules: Responsivenessreactivite, Runtimeruntime, Editorcompiler, and some smaller ones function points. So to talk about the difference between vue2 and vue3, we need to start with these three aspects and add smaller functional points.

First of all, let’s talk about Responsivenessreactivite:

vue2’s responsiveness mainly depends on Object.defineProperty Implementation, but Object.defineProperty can only monitor the getter behavior and setter behavior of the specified property of the specified object, then This can cause problems in some cases.

What's the problem?

For example: we declared an object person in data, but added new attributes to person later, then this New properties will lose responsiveness. It is actually very simple to solve this problem. You can use the Vue.$set method to increase the responsiveness of the specified attribute of the specified object. But such a method is unreasonable in Vue's automatic responsiveness mechanism.

So in Vue3, Vue introduces the concepts of reflection and proxy. The so-called reflection refers to Reflect, and the so-called proxy refers to Proxy. We can use Proxy to directly proxy a common object and get a proxy instance proxy object. In vue3, this process is implemented through the reactive method.

But proxy can only implement proxy complex data types, so vue provides an additional ref method to handle responses of simple data types sex. ref In essence, it does not monitor data, but builds a RefImpl class, which is marked # through set and get ##value Function is implemented in this way. So ref must be triggered through .value. The essence of this is to call the value method.

Next is

runtimeruntime:

The so-called runtime, most of the time refers to

renderer renderer, the renderer is essentially an object, with three main internal methods render, hydrate, createApp, among which render mainly handles rendering logic, hydrate mainly handles services End rendering logic, and createApp is the method to create vue instances.

Here we mainly talk about the

render rendering function. In order to ensure the separation of the host environment and rendering logic, all logic related to the host environment is extracted in vue3 separated, passed through the form of interface. The purpose of this is actually to unbind the host environment and rendering logic to ensure that vue can render normally in a non-browser host environment.

Further down is

Editorcompiler: compiler

in

vue is actually A DSL (dedicated language editor in a specific domain), whose purpose is to compile the template template into a render function. The logic is mainly divided into three major steps: parse, transform and generate. The function of parse is to convert template into AST (Abstract Syntax Tree) , transform can convert AST (Abstract Syntax Tree) Syntax tree) is converted into JavaScript AST, and finally generate converts JavaScript AST into render function . The transformation process will involve some slightly complex concepts, such as Finite Automatic State Machine which will not be discussed here.

In addition, there are some other changes. For example, vue3’s new composition API. composition API will have some different presentations in vue3.0 and vue3.2, for example: the original composition API starts with setup The function serves as the entry function. setup The function must return two types of values: the first is an object, and the second is a function.

When the setup function returns an object, the data or methods in the object can be used in template. When a setup function returns a function, the function is treated as a render function.

But the form of this setup function is not good, because all the logic is concentrated in the setup function, and it is easy to have a huge setup Function, we call it the Boulder (Shit Mountain) function. So in vue 3.2, a new syntax sugar of script setup was added to try to solve this problem. At present, the presentation of script setup is still very good.

In addition, there are some small changes, such as Fragment, Teleport, Suspense, etc. I won’t go into details...

Recommended learning: 《vue.js video tutorial

The above is the detailed content of The interviewer suddenly asked: What is the difference between Vue2 and Vue3?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?May 13, 2025 pm 04:05 PM

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js vs. React: Scalability and MaintainabilityVue.js vs. React: Scalability and MaintainabilityMay 10, 2025 am 12:24 AM

Vue.js and React each have their own advantages in scalability and maintainability. 1) Vue.js is easy to use and is suitable for small projects. The Composition API improves the maintainability of large projects. 2) React is suitable for large and complex projects, with Hooks and virtual DOM improving performance and maintainability, but the learning curve is steeper.

The Future of Vue.js and React: Trends and PredictionsThe Future of Vue.js and React: Trends and PredictionsMay 09, 2025 am 12:12 AM

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's Frontend: A Deep Dive into Its Technology StackNetflix's Frontend: A Deep Dive into Its Technology StackMay 08, 2025 am 12:11 AM

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js and the Frontend: Building Interactive User InterfacesVue.js and the Frontend: Building Interactive User InterfacesMay 06, 2025 am 12:02 AM

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

What are the disadvantages of VueJs?What are the disadvantages of VueJs?May 05, 2025 am 12:06 AM

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix: Unveiling Its Frontend FrameworksNetflix: Unveiling Its Frontend FrameworksMay 04, 2025 am 12:16 AM

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

Frontend Development with Vue.js: Advantages and TechniquesFrontend Development with Vue.js: Advantages and TechniquesMay 03, 2025 am 12:02 AM

Reasons for Vue.js' popularity include simplicity and easy learning, flexibility and high performance. 1) Its progressive framework design is suitable for beginners to learn step by step. 2) Component-based development improves code maintainability and team collaboration efficiency. 3) Responsive systems and virtual DOM improve rendering performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool