Vue.js is a library that provides two-way binding of MVVM data, focusing on the UI level. The core idea is: Data-driven, component system.
Data-driven:
The technical implementation of Vue.js data observation principle uses ES5Object.defineProperty and storage properties : getter and setter (so only compatible with IE9 and above), can be called an observation mechanism based on dependency collection. The core is VM, namely ViewModel, which ensures the consistency of data and views.
watcher: Each instruction will have a corresponding object used to observe data, called watcher, such as v-text="msg", {{ msg }}, which are two watchers. Among the watcher objects Contains the associated DOM elements to be rendered.
<div> {{ message }} </div> var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } })
Principle of observation mechanism based on dependency collection:
1 Transform the native data into an "observable object", usually, call defineProperty to change the data in the data object is a memory attribute. An observable object can have a value getter or a value setter.
2 When parsing the template, that is, during the evaluation process of the watcher, each observable object that is evaluated will register the current watcher as its own subscriber and become a dependency of the current watcher. .
3 When a dependent observable object is assigned a value, it will notify notify that all watchers subscribed to itself are re-evaluated and trigger corresponding updates, that is, the associated DOM change rendering in the watcher object.
The advantage of dependency collection is that it can accurately and proactively track data changes, without the need to manually trigger or evaluate all watchers in the scope (disadvantages of Angular dirty check implementation). Specially, for arrays, you need to use a variable method (such as push) that wraps the array to monitor changes in the array. When adding/deleting attributes, or modifying elements at specific positions in the array, you also need to call specific functions, such as obj.$add(key, value), to trigger updates. This is limited by the language features of ES5.
Component system:
The application UI can be regarded as entirely composed of component trees.
Register a component:
Vue.component('my-component', { // 模板 template: '<div>{{msg}} {{privateMsg}}</div>', // 接受参数 props: { msg: String }, // 私有数据,需要在函数中返回以避免多个实例共享一个对象 data: function () { return { privateMsg: 'component!' } } }) <my-component></my-component>
Core options of the component
1 Template: The template declares the data and the DOM that is ultimately displayed to the user mapping relationship between them.
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.
Webpack is an open source front-end module building tool. It provides a powerful loader API to define preprocessing logic for different file formats. This is the basis of the .vue suffix single file component form. Therefore, on this basis, the vue-loader developed by Youda allows the three elements of template, style, and logic to be integrated into the same file, forming a single-file component format with the .vue file suffix, which facilitates project architecture and development reference.
Other features:
1 Asynchronous batch DOM update: When a large amount of data changes, all affected watchers will be pushed to a queue, and each watcher The queue will only be pushed once. This queue will be executed asynchronously on the next tick of the process. This mechanism can avoid redundant DOM operations caused by multiple changes to the same data, and can also ensure that all DOM write operations are executed together to avoid layout that may be caused by DOM read and write switching.
2 Animation system: Vue.js provides a simple but powerful animation system. When the visibility of an element changes, users can not only easily define the corresponding CSS Transition or Animation effect, but also use rich JavaScript hook function for lower-level animation processing. 3 Scalability: In addition to custom directives, filters and components, Vue.js also provides a flexible mixin mechanism that allows users to reuse common features in multiple components.
The above is the detailed content of What are the two cores of vue.js. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

WebStorm Mac version
Useful JavaScript development tools

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