This article briefly summarizes and demonstrates list rendering in Vue: List rendering is done using 1. List traversalThe most basic use case 1: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>List Rendering</title> <script src="../../js/vue.js"></script> </head> <body> <div id="app"> <ul> <li v-for="(name,index) in names"> {{index}}-{{name}} </li> </ul> </div> </body> <script> new Vue({ el:'#app', data() { return { names:['Zhang San', 'Li Si', 'Wang Wu', 'Zhao Liu'] } }, }) </script> </html>
v-for can also iterate over objects, strings, specified numbers, and more. like: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>List Rendering</title> <script src="../../js/vue.js"></script> </head> <body> <div id="app"> <!-- Traverse the object--> <ul> <li v-for="(propery,key) in student"> {{key}}:{{propery}} </li> </ul> <!-- Traverse the string--> <ol> <li v-for="char in str">{{char}}</li> </ol> <!-- Custom output --> <ul> <li v-for="num in 10"> {{num}} </li> </ul> </div> </body> <script> new Vue({ el:'#app', data() { return { student: name:'Li Ming', age:23, address:'Dalian' }, str:'HelloWord' } }, }) </script> </html> The effect of the above code is as follows: 2. The role of key in Vueeffect:
Difference comparison rules:
3. List filteringList filtering is to filter the list elements before traversing the list, and select the elements that meet the requirements for display, such as: Suppose we want to filter out people named 'Zhang San' in the list: (This can be achieved using the computer or watch attributes) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>List Rendering</title> <script src="../../js/vue.js"></script> </head> <body> <div id="app"> <!-- List Filter --> <!-- computer method --> <ul> <li v-for="(name,intdex) in showNames">{{name}}</li> </ul> <!-- watch method --> <ol> <li v-for="(name,index) in displayName">{{name}}</li> </ol> </div> </body> <script> new Vue({ el:'#app', data() { return { names:['Zhang San', 'Li Si', 'Wang Wu', 'Zhao Liu'], displayName:[], } }, watch: name:{ immediate:true, handler(val){ this.displayName=this.names.filter((n)=>{ return n!='Zhang San' }) } } }, computed: { // If we want to filter out people named 'Zhang San' showNames(){ return this.names.filter((n)=>{ return n!="Zhang San" }) } }, }) </script> </html> Effect: This is the end of this article about how to use the v-for directive in vue to complete list rendering. For more information about the v-for directive in vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to build a DHCP server in Linux
>>: Mobile front-end adaptation solution (summary)
Table of contents Overview The history of CPU-bou...
Importing data with incorrect MySQL character set...
Table of contents 1. Required attributes 1. name ...
This article mainly explains how to install the M...
There are many tags in XHTML, but only a few are ...
This article uses an example to describe how to u...
Table of contents The first The second Native Js ...
The select element creates a single-select or mult...
HTML is the abbreviation of Hypertext Markup Langu...
With the rise of mobile terminals such as iPad, p...
When starting MongoDB, the prompt is: error while...
Page turning problem scenario B and C are on the ...
1. Tomcat service is not open Enter localhost:808...
Table of contents Create a simple springboot proj...
MySQL DECIMAL data type is used to store exact nu...