This article mainly shares with you the operation method of Vue.js to implement a list. Vue.js adopts the MVVM mode in design. When the View layer changes, it will automatically update to the ViewModel. Friends in need can refer to it, I hope it can help everyone.
1. Brief description of Vue.js
Vue.js (pronounced /vjuː/, similar to view) is a progressive framework for building user interfaces. Like the front-end framework Angular, Vue.js adopts the MVVM mode in design. When the View layer changes, it will automatically update to the ViewModel. Vice versa, the View and ViewModel are established through two-way data binding (data-binding). Contact, as shown in the figure below
Vue.js divides the view and data into two parts through the MVVM pattern (or decoupling the view code and business logic), so we only We need to care about data operations, DOM view updates and a series of other things, Vue will automatically handle it for us.
If two-way binding of data is implemented through the v-model instruction, the user enters any value in the input box, and the value of the message entered by the user is displayed in real time (corresponding to the above MVVM mode The relationship diagram is not difficult to understand)
<!DOCTYPE html> <html> <head> <title>Vue.js数据的双向绑定</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="https://unpkg.com/vue"></script> </head> <body> <p class="container" id="app"> <input v-model="message" placeholder="请任意输入" class="form-control"> <p>Message is: {{ message }}</p> </p> <script type="text/javascript"> new Vue({ //创建Vue实例 el:"#app", //挂载创建Vue实例对象 data: { message : "Hello Vue.js" }, methods:{} }) </script> </body> </html>
The following poster directly bypasses the basic syntax of Vue.js. If you don’t understand the basic syntax, you can consult relevant information. Starting from the case of elegant implementation of task list operations through Vue.js, we will The fragmented knowledge modules of Vue.js are integrated together.
Next, let’s experience the fresh/concise writing style of Vue.js (pronounced /vjuː/, similar to view).
2. Vue.js elegant implementation task list Operation
Vue.js To preview the elegant implementation of task list renderings, please click
## 3. HTML skeleton CSS style codeUse BootStrap The front-end responsive development framework, HTML skeleton and CSS style Demo are as follows<!DOCTYPE html> <html> <head> <title>Vue.js</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- 引入 vue.js --> <script src="https://unpkg.com/vue"></script> <style type="text/css"> .list-group button { background: none; border: 0; color: red; outline: 0; float: right; font-weight: bold; margin-left: 5px; } </style> </head> <body> <p class="container" id="app"> <p v-show="remainTask.length>0">任务列表</p> <ul class="list-group"> <li class="list-group-item"> <span title="编辑任务">Vue.js - 是一套构建用户界面的渐进式框架</span> <button title="移除任务">✗</button> <button title="任务完成">✔</button> </li> </ul> <form> <p class="form-group"> <label for="exampleInputEmail1">任务描述</label> <input type="text" class="form-control" placeholder="请输入你要添加的任务" required> </p> <p class="form-group"> <button class="btn btn-primary" type="submit">添加任务</button> </p> </form> <p>已完成的Task</p> <ol class="list-group"> <li class="list-group-item"> JavaScript高级程序设计 </li> </ol> </p> </body> </html>
<p class="container" id="app" v-cloak> <p v-show="remainTask.length>0">任务列表 ({{remainTask.length}})</p> <ul class="list-group"> <template v-for="task in remainTask"> <li class="list-group-item"> <span v-on:dblclick="editTask(task)" title="编辑任务">{{task.text}}</span> <button v-on:click="removeTask(task)" title="移除任务">✗</button> <button v-on:click="completeTask(task)" title="任务完成">✔</button> </li> </template> </ul> <form> <p class="form-group"> <label for="exampleInputEmail1">任务描述</label> <input type="text" class="form-control" placeholder="请输入你要添加的任务" v-model="newTask" required> </p> <p class="form-group"> <button class="btn btn-primary" type="submit" v-on:click="addTask">添加任务</button> </p> </form> <p>已完成的Task({{filterTask.length}})</p> <ol class="list-group"> <template v-for="task in filterTask"> <li class="list-group-item"> {{task.text}} </li> </template> </ol> </p> <script type="text/javascript"> var app = new Vue({ //创建Vue对象实例 el:"#app", //挂载DOM元素的ID data: { tasks : [ { text : "Vue.js - 是一套构建用户界面的渐进式框架", complete:false}, { text : "Bootstrap 响应式布局", complete:false }, { text : "Webpack前端资源模块化管理和打包工具", complete:false}, { text : "Yarn 中文手册Yarn 是一个快速、可靠、安全的依赖管理工具", complete:true}, { text : "JavaScript语言精粹", complete:false}, { text : "JavaScript高级程序设计", complete:true} ], newTask:"程序员的修炼之道" //默认值 }, methods:{ addTask:function(event){ //添加任务 event.preventDefault(); this.tasks.push({ text: this.newTask, complete: false }); this.newTask = ""; }, editTask:function(task){ //编辑任务 //移除当前点击task this.removeTask(task); //更新vue实例中newTask值 this.newTask = task.text; }, removeTask: function(task){ //删除任务 //指向Vue实例中的tasks _tasks = this.tasks; //remove _tasks.forEach(function(item, index){ if(item.text == task.text){ _tasks.splice(index, 1); } }) }, completeTask: function(task){ //任务完成状态 task.complete = true; //设置任务完成的状态 } }, //用于计算属性,属性的计算是基于它的依赖缓存(如vue实例中的tasks) //只有当tasks数据变化时,才会重新取值 computed:{ remainTask:function(){ //筛选未完成的记录 return this.tasks.filter(function(task){ //filter过滤器 return !task.complete; }) }, filterTask:function(){ //筛选已完成的记录 return this.tasks.filter(function(task){ return task.complete; }) } } }); </script>
<p id="app"> <p class="collection"> <a href="#!" class="collection-item" v-for="gameName in gameNames" :class="{active: activeName == gameName}" @click="selected(gameName)">{{gameName}}</a> </p> </p>
new Vue({ el: "#app", data: { gameNames: ['魔兽世界', '暗黑破坏神Ⅲ', '星际争霸Ⅱ', '炉石传说', '风暴英雄', '守望先锋' ], activeName: '' }, methods: { selected: function(gameName) { this.activeName = gameName } } })
Layout tags and list tags in html. Detailed graphic and text explanation
Usage summary of custom list item list-style
Introduction to how JavaScript implements automatic scrolling of Select list content
The above is the detailed content of Detailed explanation of how Vue.js implements lists. For more information, please follow other related articles on the PHP Chinese website!