Home >Web Front-end >Vue.js >A brief analysis of Vue's list rendering instructions: v-for
This article will take you through Vue’s list rendering command: v-for. I hope it will be helpful to you!
Personally, I feel that it is actually a for loop with basic syntax. The usage is similar, but the form is different. If you understand it, you will be able to use it. (Learning video sharing:
vue video tutorial)
v-for="item in 数组"
v-for="(item, index) in 数组"Example of traversing an array:
<div class="item" v-for="item in products"> <h3 class="title">商品:{{item.name}}</h3> <span>价格:{{item.price}}</span> <p>秒杀:{{item.desc}}</p> </div> const app = Vue.createApp({ data() { return { //2.数组 存放的是对象 products: [ { id: 11, name: "mac", price: 1000, desc: "99" }, ], }; }, }); app.mount("#app");2. Traversing objects
⭐⭐
v-for also supports traversing objects, and supports one, two or three parameters:
Each item is a number;
<!-- 2.遍历对象 --> <ul> <li v-for="(value,key,index) in info"> {{value}} - {{key}} - {{index}} </li> </ul> const app = Vue.createApp({ data() { return { info: { bame: "why", age: 18, height: 1.88 }, }; }, }); app.mount("#app");3. Traversing strings
<li v-for="item in 100">{{item}}</li>
We can use the template element to loop through rendering a piece of content containing multiple elements
Why not here What about using div?
I didn’t think much about this when I was studying before. I discovered this problem when I was sorting out my notesReason:
In fact, the function of template is a template placeholder, which can help us wrap elements. During the loop process, template will not be rendered to the page.
0a0b09a86095b895314008cac4ff62a7 45a2772a6b6107b401db3c9b82c049c2{{value}}54bdf357c58b8a65c66d7c19c8e4d114 8e99a69fbe029cd4e2b854e244eab143{{key}}128dba7a3a77be0113eb0bea6ea0a5d0 5a8028ccc7a7e27417bff9f05adf5932{{index}}72ac96585ae54b6ae11f849d2649d9e6 16b28748ea4df4d9c2150843fecfba68
<template v-for="(value,key,index) in infos"> <span>{{value}}</span> <strong>{{key}}</strong> <i>{{index}}</i> </template>
:
push() Insert an element from the back of the arrayThe above method will directly modify the original array;
//并不是完整写法!!! <li v-for="item in names">{{item}}</li> names: ["abc", "bac", "aaa", "cbb"], // 1.直接将数组修改为一个新的数组 this.names = ["cc", "kk"]; // 2.通过一些数组的方法,修改数组中的元素 this.names.push("cc"); this.names.pop(); this.names.splice(2, 1, "cc"); this.names.sort(); this.names.reverse();
The above is the detailed content of A brief analysis of Vue's list rendering instructions: v-for. For more information, please follow other related articles on the PHP Chinese website!