The use method and related skills of loop statement in Vue.js

1 Overview

In the development of Vue.js, the loop statement is one of the very commonly used grammars. Through loop statements, we can traverse arrays and objects, dynamically generate repeated HTML elements or perform a series of operations. This article will introduce in detail the usage and related skills of loop statements in Vue.js.

2. v-for instruction

The v-for instruction is the most basic loop statement instruction in Vue.js. It is used to traverse arrays or objects, and generate corresponding HTML elements or execute a piece of code according to each element. The basic syntax of the v-for directive is as follows:

<div v-for="item in list" :key="item.id">{
   
   { item.name }}</div>

In the above code, list is an array, and item is each element in the array. The v-for instruction traverses each element in the array and generates a

element. Use the :key directive to set a unique identifier for each element generated by the loop, which can improve performance and avoid rendering errors.

The v-for directive also supports getting the index of the current element and the attributes of the parent element. Here is an example:

<div v-for="(item, index) in list" :key="item.id">
  {
   
   { index }} - {
   
   { item.name }}
</div>

In the above code, index represents the index of the current element, and item represents the value of the current element.

The v-for instruction also supports looping on objects, which can traverse the properties and values ​​​​of objects. Here is an example:

<div v-for="(value, key) in object" :key="key">
  {
   
   { key }}: {
   
   { value }}
</div>

In the above code, key represents the property name of the object, and value represents the property value of the object.

3. Loop nesting

In Vue.js, loop statements can be nested to achieve multi-level loop traversal. For example, a loop can be nested within a loop to traverse a two-dimensional array. Here is an example:

<div v-for="row in matrix" :key="row.id">
  <div v-for="cell in row" :key="cell.id">
    {
   
   { cell.value }}
  </div>
</div>

In the above code, matrix is ​​a two-dimensional array, row represents each row, and cell represents each cell. Values ​​in a two-dimensional array can be rendered row-by-row and cell-by-cell through nested loop statements.

4. Loop filtering and sorting

When using the v-for command, you can also filter and sort the array, so as to filter out the required elements or adjust the order of the elements according to certain conditions. Here is an example:

<div v-for="item in list.filter(item => item.price > 100)" :key="item.id">
  {
   
   { item.name }} - {
   
   { item.price }}
</div>

In the above code, the filter() method is used to filter out the elements whose price is greater than 100, and render the qualified elements.

<div v-for="item in sortedList" :key="item.id">
  {
   
   { item.name }} - {
   
   { item.price }}
</div>

In the above code, sortedList is a sorted array. By sorting the array, the order of the elements can be adjusted and rendered according to the sorted result.

5. Event handling in the loop

In the loop statement, it is often necessary to bind event handlers to the generated HTML elements. For example, with a list, it might be necessary to add a click event to each list item. In Vue.js, you can use the index or unique identifier of the loop as a parameter to pass to the event handler function. Here is an example:

<div v-for="(item, index) in list" :key="item.id" @click="handleItemClick(index)">
  {
   
   { item.name }}
</div>

In the above code, the handleItemClick() method is an event processing function, which receives the index as a parameter, and performs corresponding operations according to the index.

6. Summary

Loop statements are a very important part of Vue.js. It can traverse arrays and objects, dynamically generate repeated HTML elements or perform a series of operations. This article introduces in detail the usage and related techniques of loop statements in Vue.js, including the basic usage of v-for directives, nesting of loops, filtering and sorting of loops, and event handling in loops. I hope that through the introduction of this article, you have a deeper understanding and mastery of loop statements in Vue.js. In actual development, reasonable and flexible use of loop statements can help us build more interactive and maintainable applications.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/131788642