The two cores of vuejs are data-driven and component systems. Data-driven is two-way binding of data, which is used to ensure the consistency of data and views. The component system can abstract the page into multiple relatively independent modules; it can realize code reuse, improve development efficiency and code quality, and facilitate code maintenance.
The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.
The two cores of vue.js: 1. Data-driven------------- 2. Component system
1. Data Driver, that is, two-way binding of data
After the data changes, the page will be re-rendered. This is Vue responsiveness. So how is all this done?
To complete this process, we need to:
- Detect data changes
- Collect what data the view depends on
- When the data changes, Automatically "notify" the view part that needs to be updated and update it
The corresponding professional sayings are:
- Data hijacking/data agent
- Dependency collection
- Publish and subscribe mode
In other words: The core of Vue's responsiveness is that dependencies will be collected during getters and dependency updates will be triggered during setters.
vue will Traverse all the properties of the objects in the data, and use Object.defineProperty
to convert all these properties into getter/setter.
These getters/setters are invisible to the user, but internally they allow Vue to track dependencies and notify changes when the property is accessed and modified .
Each component instance corresponds to a watcher instance, which records the "touched" data properties as dependencies during the component rendering process.
We will collect dependencies during getter. Dependency collection is the collection of subscription data change watchers. The purpose of dependency collection is to when the responsive data changes, it can notify the corresponding subscribers. Process related logic.
setter will trigger a dependency update. Later, when the dependency's setter is triggered, will notify the watcher, so that its associated components will be re-rendered. .
Summary:
1) Principle:
When creating a Vue instance, vue will traverse the properties of the data option and use Object.defineProperty as the property Add getters and setters to hijack data reading (getters are used for dependency collection and setters are used to dispatch updates), and track dependencies internally to notify changes when properties are accessed and modified.
Each component instance will have a corresponding watcher instance, which will record all the data attributes of dependencies during the component rendering process (dependency collection, as well as computed watcher and user watcher instances), and then the dependencies are changed When the time comes, the setter method will notify the watcher instance that depends on this data to recalculate (dispatch updates),
, thereby re-rendering its associated component.
2) Implementation process:
We already know that to implement two-way binding of data, we must first hijack and monitor the data, so we need to set up an Observer to monitor all properties. If the attribute changes, you need to tell the subscriber Watcher to see if it needs to be updated.
Because there are many subscribers, we need to have a message subscriber Dep to specifically collect these subscribers, and then manage them uniformly between the listener Observer and the subscriber Watcher. Next, we also need an instruction parser Compile to scan and parse each node element,
correspondingly initialize the relevant instructions into a subscriber Watcher, and replace the template data or bind the corresponding function. At this time, when the subscriber Watcher receives the change of the corresponding attribute, it will execute the corresponding update function to update the view. Therefore, we next perform the following three steps to achieve two-way binding of data:
1. Implement a listener Observer to hijack and monitor all properties, and notify subscribers if there are changes.
2. Implement a subscriber Watcher that can receive property change notifications and execute corresponding functions to update the view.
3. Implement a parser Compile, which can scan and parse the relevant instructions of each node, and initialize the template data and initialize the corresponding subscriber
Note: Proxy
is a new feature of JavaScript 2015. Proxy
's proxy is for the entire object, not a certain property of the object, so it is different from Object.defineProperty
which must traverse each property of the object,Proxy
Only one layer of proxy is needed to monitor all attribute changes under the same level structure. Of course, for deep structures, recursion still needs to be performed. In additionProxy
supports changes in the proxy array. Proxy
is the method used by vue3.0
2. Component system
Understanding:
1) Able to abstract the page into multiple relatively independent modules;
2) Implement code reuse, improve development efficiency and code quality, and facilitate code maintenance
components Core options
1 Template: The template declares the mapping relationship between the data and the DOM that is ultimately displayed to the user.
2 Initial data (data): The initial data state of a component. For reusable components, this is usually private state.
3 Accepted external parameters (props): Data is transferred and shared between components through parameters.
4 Methods: Data modification operations are generally performed within the component’s methods.
5 Lifecycle hooks: A component will trigger multiple lifecycle hook functions. The latest version 2.0 has greatly changed the name of the lifecycle function.
6 Private resources (assets): In Vue.js, user-defined instructions, filters, components, etc. are collectively called resources. A component can declare its own private resources. Private resources can only be called by the component and its subcomponents.
Related recommendations: "vue.js Tutorial"
The above is the detailed content of What are the two cores of vuejs. For more information, please follow other related articles on the PHP Chinese website!

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 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 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 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.

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 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.

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.

Vue.js is easier to use and has a smooth learning curve, which is suitable for beginners; React has a steeper learning curve, but has strong flexibility, which is suitable for experienced developers. 1.Vue.js is easy to get started with through simple data binding and progressive design. 2.React requires understanding of virtual DOM and JSX, but provides higher flexibility and performance advantages.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
