search
HomeWeb Front-endVue.jsLet's talk about how to apply the MVVM model in Vue

Let's talk about how to apply the MVVM model in Vue

Oct 25, 2022 pm 09:36 PM
vuejavascriptvue.jsvue3

Let's talk about how to apply the MVVM model in Vue

Understanding the MVVM model

We know that everyVue application starts by creating a new instance, according to the official documentation of Vue2, we can know that Vue’s design is inspired by the MVVM model, so when we create a Vue instance, the document The variable name vm is often used to represent the Vue instance. (Learning video sharing: vue video tutorial)

What is the MVVM model

We know that Vue draws on the principles of the MVVM model, but we I only know how to learn from this, but I don’t know what the MVVM model is. Here is a brief explanation of what the MVVM model is.

MVVM (Model-View-ViewModel) abstracts the state and behavior of the View. The MVVM mode is the same as the MVC mode. The main purpose is to separate the view and the model. MVVM aims to use the data binding in WPF. Defined functions better facilitate the separation of view layer development from the rest of the schema by removing virtually all GUI code from the view layer, eliminating the need for user experience developers to write GUI code, they can use the framework markup language and create it into the application Data binding for view models written and maintained by developers. As shown in the figure below:

The MVVM model is mainly to separate the view (View) and the model (Model). Its advantages are: low coupling, reusability, independent development and Testable. The separation of view and model inspired Vue a lot.

Components of MVVM

The components in the MVVM pattern are divided into the following four types:

Model: represents the content of the real state, that is, the data access layer (including data entities and operations of data entities)

View(View) : The structure, layout and appearance that users can see on the screen, responsible for data display and interaction

ViewModel (view model) : Exposed public The abstraction of properties and named views binds the Model and View so that they can be refreshed in real time when data changes. ViewModel can observe changes in data and update the content corresponding to the view; ViewModel can monitor changes in the view and notify data of changes.

Binder: In the view model, communicate between the view and the data binder.

Implementation in Vue

MVVM is Model-View-ViewModel. The model refers to the data passed by the backend; the view refers to the page you see. The view model is the core of the mvvm pattern. It is the bridge connecting the view and the model. It has two directions: one is to convert the model into a view, that is, convert the data passed by the backend into the page you see. The way to achieve this is: data binding. The second is to convert the view into the model, that is, convert the page you see into the back-end data. The way to implement it is: DOM event listening. Implementing both directions is called two-way binding of data.

So the core of Vue, which is inspired by the MVVM model, is to implement DOM monitoring and event binding, as shown in the following classic illustration:

Application of MVVM model in Vue

Give a simple Vue case to illustrate what the three main components of MVVM represent in Vue. , as follows:

<body>
    <div id="id">
        <h1 id="姓名-name">姓名:{{name}}</h1>
        <h1 id="国籍-address">国籍:{{address}}</h1>
    </div>
    <script>
        Vue.config.productionTip = false; //阻止 vue 在启动时生成生产提示
        const vm = new Vue({
           data:{
                name:&#39;张三&#39;,
                address:&#39;中国&#39;
            }
        })
        vm.$mount(&#39;#id&#39;)
        console.log(vm);
    </script>
</body>

##Model: Model layer, the data in data represents the JS object

View: View layer, HTML content part, representing DOM elements that can be operated in HTML

ViewModel: Acts as the middleman connecting the view and the data, that is, it defines the identity of the Observer observer, that is, the bridge

Of course the idea of ​​the MVVM model Not only is it applied to Vue, other mainstream front-end frameworks can also see the shadow of the MVVM model. I hope that after reading this article, it will help you have a deeper understanding of the MVVM model.

[Related video tutorial recommendations: vuejs entry tutorial, web front-end entry]

The above is the detailed content of Let's talk about how to apply the MVVM model in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
What Happens When the Vue.js Virtual DOM Detects a Change?What Happens When the Vue.js Virtual DOM Detects a Change?May 14, 2025 am 12:12 AM

WhentheVue.jsVirtualDOMdetectsachange,itupdatestheVirtualDOM,diffsit,andappliesminimalchangestotherealDOM.ThisprocessensureshighperformancebyavoidingunnecessaryDOMmanipulations.

How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?May 13, 2025 pm 04:05 PM

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js vs. React: Scalability and MaintainabilityVue.js vs. React: Scalability and MaintainabilityMay 10, 2025 am 12:24 AM

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 of Vue.js and React: Trends and PredictionsThe Future of Vue.js and React: Trends and PredictionsMay 09, 2025 am 12:12 AM

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 Frontend: A Deep Dive into Its Technology StackNetflix's Frontend: A Deep Dive into Its Technology StackMay 08, 2025 am 12:11 AM

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 and the Frontend: Building Interactive User InterfacesVue.js and the Frontend: Building Interactive User InterfacesMay 06, 2025 am 12:02 AM

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.

What are the disadvantages of VueJs?What are the disadvantages of VueJs?May 05, 2025 am 12:06 AM

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: Unveiling Its Frontend FrameworksNetflix: Unveiling Its Frontend FrameworksMay 04, 2025 am 12:16 AM

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.

See all articles

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

Video Face Swap

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

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.