Home Web Front-end JS Tutorial What are the two cores of vue.js

What are the two cores of vue.js

May 29, 2019 am 10:58 AM
vue.js

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.

What are the two cores of vue.js

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 id="app">
  {{ message }}
</div>
var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  }
})
Copy after login

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 msg="hello"></my-component>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In-depth discussion of how vite parses .env files In-depth discussion of how vite parses .env files Jan 24, 2023 am 05:30 AM

When using the Vue framework to develop front-end projects, we will deploy multiple environments when deploying. Often the interface domain names called by development, testing and online environments are different. How can we make the distinction? That is using environment variables and patterns.

Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Apr 24, 2023 am 10:52 AM

Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.

What is the difference between componentization and modularization in vue What is the difference between componentization and modularization in vue Dec 15, 2022 pm 12:54 PM

The difference between componentization and modularization: Modularization is divided from the perspective of code logic; it facilitates code layered development and ensures that the functions of each functional module are consistent. Componentization is planning from the perspective of UI interface; componentization of the front end facilitates the reuse of UI components.

Let's talk in depth about reactive() in vue3 Let's talk in depth about reactive() in vue3 Jan 06, 2023 pm 09:21 PM

Foreword: In the development of vue3, reactive provides a method to implement responsive data. This is a frequently used API in daily development. In this article, the author will explore its internal operating mechanism.

Explore how to write unit tests in Vue3 Explore how to write unit tests in Vue3 Apr 25, 2023 pm 07:41 PM

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

A brief analysis of how to handle exceptions in Vue3 dynamic components A brief analysis of how to handle exceptions in Vue3 dynamic components Dec 02, 2022 pm 09:11 PM

How to handle exceptions in Vue3 dynamic components? The following article will talk about Vue3 dynamic component exception handling methods. I hope it will be helpful to everyone!

A simple comparison of JSX syntax and template syntax in Vue (analysis of advantages and disadvantages) A simple comparison of JSX syntax and template syntax in Vue (analysis of advantages and disadvantages) Mar 23, 2023 pm 07:53 PM

In Vue.js, developers can use two different syntaxes to create user interfaces: JSX syntax and template syntax. Both syntaxes have their own advantages and disadvantages. Let’s discuss their differences, advantages and disadvantages.

A brief analysis of how vue implements file slicing upload A brief analysis of how vue implements file slicing upload Mar 24, 2023 pm 07:40 PM

In the actual development project process, sometimes it is necessary to upload relatively large files, and then the upload will be relatively slow, so the background may require the front-end to upload file slices. It is very simple. For example, 1 A gigabyte file stream is cut into several small file streams, and then the interface is requested to deliver the small file streams respectively.

See all articles