What are the two cores of vuejs
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Some tips for developing Android applications using Vue.js and Kotlin language. With the popularity of mobile applications and the continuous growth of user needs, the development of Android applications has attracted more and more attention from developers. When developing Android apps, choosing the right technology stack is crucial. In recent years, Vue.js and Kotlin languages have gradually become popular choices for Android application development. This article will introduce some techniques for developing Android applications using Vue.js and Kotlin language, and give corresponding code examples. 1. Set up the development environment at the beginning

Some tips for developing data visualization applications using Vue.js and Python Introduction: With the advent of the big data era, data visualization has become an important solution. In the development of data visualization applications, the combination of Vue.js and Python can provide flexibility and powerful functions. This article will share some tips for developing data visualization applications using Vue.js and Python, and attach corresponding code examples. 1. Introduction to Vue.js Vue.js is a lightweight JavaScript

How to use Vue to implement QQ-like chat bubble effects In today’s social era, the chat function has become one of the core functions of mobile applications and web applications. One of the most common elements in the chat interface is the chat bubble, which can clearly distinguish the sender's and receiver's messages, effectively improving the readability of the message. This article will introduce how to use Vue to implement QQ-like chat bubble effects and provide specific code examples. First, we need to create a Vue component to represent the chat bubble. The component consists of two main parts

The integration of Vue.js and Lua language, best practices and experience sharing for building a front-end engine for game development Introduction: With the continuous development of game development, the choice of game front-end engine has become an important decision. Among these choices, the Vue.js framework and Lua language have become the focus of many developers. As a popular front-end framework, Vue.js has a rich ecosystem and convenient development methods, while the Lua language is widely used in game development because of its lightweight and efficient performance. This article will explore how to

How to use PHP and Vue.js to implement data filtering and sorting functions on charts. In web development, charts are a very common way of displaying data. Using PHP and Vue.js, you can easily implement data filtering and sorting functions on charts, allowing users to customize the viewing of data on charts, improving data visualization and user experience. First, we need to prepare a set of data for the chart to use. Suppose we have a data table that contains three columns: name, age, and grades. The data is as follows: Name, Age, Grades Zhang San 1890 Li

Use Vue.js and Kotlin language to develop mobile application solutions with international support. As the process of globalization accelerates, more and more mobile applications need to provide multi-language support to meet the needs of global users. During the development process, we can use Vue.js and Kotlin languages to implement internationalization functions so that the application can run normally in different language environments. 1. Vue.js international support Vue.js is a popular JavaScript framework that provides a wealth of tools and features.

Use Vue.js and Perl languages to develop efficient web crawlers and data scraping tools. In recent years, with the rapid development of the Internet and the increasing importance of data, the demand for web crawlers and data scraping tools has also increased. In this context, it is a good choice to combine Vue.js and Perl language to develop efficient web crawlers and data scraping tools. This article will introduce how to develop such a tool using Vue.js and Perl language, and attach corresponding code examples. 1. Introduction to Vue.js and Perl language

Integration of Vue.js and Dart language, ideas for building cross-platform mobile applications In the field of mobile application development, cross-platform development frameworks have received more and more attention. Vue.js is a JavaScript framework for building user interfaces, while Dart is a language developed by Google for building cross-platform applications. This article will explore how to integrate Vue.js with the Dart language to build cross-platform mobile applications. 1. Introduction to Vue.js Vue.js is considered a lightweight
