Home >Web Front-end >JS Tutorial >VueJS MVVM model compared to Pure HTML
Original article: http://blog.kueiapp.com/programming-tw/vuejs-mvvm-VueJS-MVVM-Model and -Pure-HTML5-Comparison/
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.
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]; } }); }); });
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.
<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: [], }), });
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!