Home > Web Front-end > JS Tutorial > body text

VueJS MVVM model compared to Pure HTML

王林
Release: 2024-08-21 10:33:36
Original
1188 people have browsed it

Original article: http://blog.kueiapp.com/programming-tw/vuejs-mvvm-VueJS-MVVM-Model and -Pure-HTML5-Comparison/

The difference between Vue.js and Native HTML5

There are many methods and frameworks to build web front-end systems, such as React, AngularJS, Vue.js, etc. It's absolutely possible to build a system using native HTML5 and JavaScript, but usually using a good framework can implement the system with better design patterns in terms of structure and security.

A tool or architecture is usually created because developers have encountered certain difficulties. For example, jQuery was invented to use JavaScript on browsers that support different standards, and it was the first library to control the DOM (Document Object Modal) using CSS selectors. The HTML5 querySelector replicates this cool feature to make it easier for HTML to manipulate the DOM.

Why you need VueJS

When using Pure HTML5 JavaScript to access the DOM to grab data from the HTML tree, there is a lot of work that needs to be done, such as targeting elements, adding listeners to detect user interactions on the UI, or when the target action is captured Returning data when retrieving it is actually very resource intensive.

<html>
  <body>
    <h1>Checkbox with pure HTML5 syntax</h1>
    <label>
      <input type="checkbox" name="group1" value="check1" />
      checkbox 1
    </label>
    <label>
      <input type="checkbox" name="group1" value="check2" />
      checkbox 2
    </label>
  </body>
</html>
<script>
window.onload = ()=>{
  const group1: NodeListOf<HTMLInputElement> | undefined =
      document.querySelectorAll("input[name=group1]");
    if (!group1) return;
    group1.forEach((checkbox) => {
      checkbox.addEventListener("change", (e: Event) => {
        if (!e.target) return;
        const targetValue = (e.target as HTMLInputElement).value.toString();
        const checked = (e.target as HTMLInputElement).checked;
        if (!checked) {
          this.selected = this.selected.filter(
            (select) => select !== targetValue
          );
        } else {
          this.selected = [...this.selected, targetValue];
        }
      });
  });
});
Copy after login

VueJS MVVM 模型與 Pure HTML比較

Compared with native HTML5 JavaScript (or jQuery) that directly operates the DOM, Vue.js has its own MVVM model that can modify the DOM while obtaining a new data model. In other words, we can focus on data structure rather than designing our own model to directly manipulate the DOM.

Both approaches are suitable for different situations, but Vue.js actually provides an easier path to building a web frontend.

Using Vue.js

<div>      
      <label>
        check 1
        <input type="checkbox" v-model="checkboxList" value="1" />
      </label>
      <label>
        check 2
        <input type="checkbox" v-model="checkboxList" value="2" />
      </label>
</div>
export default Vue.extend({
  data: () => ({
    checkboxList: [],
  }),
});
Copy after login

example

https://github.com/kueiapp/vue-typescript-tutorial/blob/main/src/App.vue

Original article: http://blog.kueiapp.com/programming-tw/vuejs-mvvm-VueJS-MVVM-Model and -Pure-HTML5-Comparison/

The above is the detailed content of VueJS MVVM model compared to Pure HTML. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template