Notes on Vue.js and MVVM
MVVM is the abbreviation of Model-View-ViewModel. It is an architectural pattern based on front-end development. Its core is to provide two-way data binding between View and View Model, which allows the state changes of View Model to be automatically passed to View. This is called two-way data binding.
Vue.js is a Javascript library that provides MVVM-style two-way data binding, focusing on the View layer. Its core is the VM in MVVM, which is ViewModel. ViewModel is responsible for connecting View and Model to ensure the consistency of view and data. This lightweight architecture makes front-end development more efficient and convenient.
Why does MVVM appear?
I came into contact with MVVM in 2015. It can be said that 2015 was the hottest year for MVVM. Before that, all I knew was MVC. I came into contact with MVC about 5 years ago, that is, in 2011. , I just learned programming language at that time, and I learned Java, and the classic SSH framework in Java was used to build a standard MVC architecture. To be honest, I have been using the MVC architecture for so many years, but I have never had a deep understanding of it. I only stayed at the level of use. It was not until I came into contact with Vue.js, studied the MVVM architectural ideas, and then looked back at MVC that I suddenly became enlightened. The feeling~
MVC is the abbreviation of Model-View-Controller, which is Model-View-Controller. In other words, a standard Web application is composed of these three parts:
View is used to convert data in some way. The way is presented to the user
Model is actually data
Controller receives and processes requests from users, and returns the Model to the user
In those years when HTML5 was not yet popular, MVC was OK as the best practice for web applications Yes, this is because the View layer of the Web application is relatively simple, and the data required by the front end can basically be processed by the back end. The View layer is mainly for display. At that time, Controller was advocated to handle complex business. logic, so the View layer is relatively lightweight, which is the so-called thin client idea.
From 2010 to 2011, the concept of HTML5 was hyped and sought after. In 2012, W3C officially announced that the HTML5 specification had been officially finalized. In 2013, when I first joined the company, I came into contact with Sench touch, an HTML5 framework. Sench touch is an HTML5 framework used to build mobile applications. It completely separates the front and back ends. The front end adopts the MVC architecture and is used as an independent project. maintain.
Why does the front-end need to be engineered if MVC is used?
Compared with HTML4, the biggest highlight of HTML5 is that it provides some very useful functions for mobile devices, making HTML5 capable of developing Apps. The biggest advantage of developing Apps with HTML5 is cross-platform, rapid iteration and launch, saving labor costs and Submission efficiency, so many companies began to transform traditional apps and gradually replaced Native pages with H5. By 2015, many apps on the market had more or less embedded H5 pages.
Since we want to use H5 to build the App, what the View layer does is not just simple data display. It must manage data, manage various statuses of user operations, and also handle various user operations on mobile devices. behavior and so on. Therefore, the front end also needs a framework similar to MVC to manage these complex logics and make development more efficient. But at this time, MVC has slightly changed:
View UI layout, display data
Model manages data
Controller responds to user operations and updates the Model to the View
This MVC architecture pattern is suitable for basic applications It seems OK, and it is in line with the layered thinking of software architecture. But in fact, with the continuous development of H5, people hope that applications developed using H5 can be comparable to Native, or close to the native App experience. Therefore, the complexity of front-end applications is no longer what it used to be. At this time, the front end exposed three important pain points:
1. Developers call a large number of the same DOM API in the code, which makes the processing cumbersome and redundant, making the code difficult to maintain.
2. A large number of DOM operations reduce the page rendering performance and slow down the loading speed, affecting the user experience.
3. When the Model changes frequently, developers need to actively update the View; when user operations cause the Model to change, developers also need to synchronize the changed data to the Model.
Such work is not only cumbersome, but also difficult Maintain complex and changing data status.
In fact, the early emergence of jquery was for the front-end to operate the DOM more concisely, but it only solved the first problem, and the next two problems always existed with the front-end.
The emergence of MVVM perfectly solves the above three problems.
MVVM is composed of three parts: Model, View, and ViewModel. The Model layer represents the data model, and business logic for data modification and operation can also be defined in the Model; View represents the UI component, which is responsible for converting the data model into UI display, and ViewModel It is an object that synchronizes View and Model.
Under the MVVM architecture, there is no direct connection between View and Model. Instead, they interact through ViewModel. The interaction between Model and ViewModel is two-way, so changes in View data will be synchronized to the Model, and Model data Changes will also be immediately reflected on the View.
ViewModel connects the View layer and Model layer through two-way data binding, and the synchronization between View and Model is completely automatic without human intervention, so developers only need to focus on business logic and do not need to manually operate the DOM. There is no need to pay attention to the synchronization of data status. Complex data status maintenance is completely managed by MVVM.
Details of Vue.js
Vue.js can be said to be the best practice of MVVM architecture. It focuses on ViewModel in MVVM. It not only achieves two-way data binding, but is also a relatively lightweight JS library. , the API is simple and easy to use. There are ready-made tutorials on the Internet for the basic knowledge of Vue, so I won’t go into details here. Let’s take a brief look at some implementation details of Vue.js about two-way binding:
Vue.js uses the getter and setter of Object.defineProperty, and combines them Observer pattern is used to implement data binding. When you pass a plain Javascript object to a Vue instance as its data option, Vue will iterate through its properties and convert them into getters/setters using Object.defineProperty. The getters/setters are not visible to the user, but internally they allow Vue to track dependencies and notify changes when properties are accessed and modified.
Observer data listener, which can monitor all properties of the data object. If there is any change, it can get the latest value and notify subscribers. It is implemented internally using the getter and setter of Object.defineProperty
Compile instruction parser , its role is to scan and parse the instructions of each element node, replace the data according to the instruction template, and bind the corresponding update function
Watcher subscriber, as a bridge connecting Observer and Compile, can subscribe to and receive each Notification of attribute changes, execute the corresponding callback function bound by the instruction
Dep message subscriber, internally maintains an array to collect subscribers (Watchers), data changes trigger the notify function, and then call the update method of the subscriber
When new Vue() is executed, Vue enters the initialization phase. On the one hand, Vue will traverse the properties in the data option and use Object.defineProperty to convert them into getters/setters to implement the data change monitoring function; on the other hand, Vue's The instruction compiler Compile scans and parses the instructions of the element node, initializes the view, and subscribes to Watcher to update the view. At this time, Water will add itself to the message subscriber (dep), and the initialization is completed.
When the data changes, the setter method in the Observer is triggered. The setter will immediately call Dep.notify(). Dep starts to traverse all subscribers and calls the update method of the subscriber. After the subscriber receives the notification, it will update the view. Make corresponding updates and complete a data binding.

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

With the rapid development of web applications, more and more developers are turning their attention to various emerging web development frameworks and architectural design patterns. One of the high-profile design patterns is the MVVM (ModelViewViewModel) architectural pattern. MVVM adopts a modern design pattern that allows developers to better manage and maintain applications by separating UI and business logic. In addition, MVVM reduces unnecessary coupling and improves code reusability and flexibility.

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.

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.

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.

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.

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.

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.

There are two ways to query the current Vue version: 1. In the cmd console, execute the "npm list vue" command to query the version. The output result is the version number information of Vue; 2. Find and open the package.json file in the project and search You can see the version information of vue in the "dependencies" item.
