How to use v-for instruction to loop output data in Vue
Vue is a popular front-end framework that provides many powerful directives to easily manage views and data. One of the most commonly used instructions is v-for, which can loop through data and output it. In this article, we will learn how to loop through data using the v-for directive in Vue.
First, let’s take a look at the usage of v-for. The basic syntax is as follows:
<template> <ul> <li v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> </template>
In the above code, we use the v-for
instruction to loop through the data in the list
array and output it as li elements. The v-for
instruction consists of three parts: v-for="(item, index) in list"
. Among them, (item, index)
is the defined item and index, which can be changed according to needs. list
is the data array being looped.
key
The attribute is required and needs to be unique. It is used by Vue to identify DOM elements in order to render the component faster. Vue will warn if the key attribute is not set, so be sure to set the key attribute.
Next, let’s look at some specific examples to show how to use the v-for instruction to loop out data in Vue.
1. Traverse the array and output a list
In this example, we will use the v-for instruction to loop through an array and output it as a list. In this example, we use the spread operator in ES6 to spread the array.
<template> <div> <ul> <li v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> </div> </template> <script> export default { data() { return { list: ['foo', 'bar', 'baz'] } } } </script>
In the above example, we created an array and passed it to the data property of the Vue instance. Next, we use the v-for directive to loop through each item in the array and render it as a list.
2. Traverse the object and output a list
In this example, we will use the v-for instruction to loop through an object and output it as a list. We used the key and value in the object to display the list items and set the key property to the name of the key.
<template> <div> <ul> <li v-for="(value, key, index) in obj" :key="key">{{ key }} - {{ value }}</li> </ul> </div> </template> <script> export default { data() { return { obj: { foo: 'Foo', bar: 'Bar', baz: 'Baz' } } } } </script>
In the above example, we created an object and passed it to the data property of the Vue instance. We then use the v-for directive to loop through each key-value pair in the object and render them as a list.
3. Loop through numbers and output a list
In this example, we will use the v-for instruction to loop through a number and output it as a list. In v-for we loop the numbers into an array and then count using the index of the item in the array.
<template> <div> <ul> <li v-for="(item, index) in 5" :key="index">{{ index + 1 }}</li> </ul> </div> </template>
In the above example, we use the v-for instruction to loop the numbers into an array of length 5 and output it as a list. We use the index variable to count and add it to 1 to get the sequence number.
4. Traverse the array and output the table
In this example, we will use the v-for instruction to loop through an array and output it as an HTML table. We used computed properties to split the nested array into individual data items.
<template> <div> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> </thead> <tbody> <tr v-for="(item, index) in flattenedList" :key="index"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.gender }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { list: [ { name: 'Alice', age: 22, gender: 'Female' }, { name: 'Bob', age: 25, gender: 'Male' }, { name: 'Charlie', age: 30, gender: 'Male' }, { name: 'Diana', age: 27, gender: 'Female' } ] } }, computed: { flattenedList() { return this.list.flatMap(item => [item.name, item.age, item.gender]) } } } </script>
In the above example, we created an array of four objects and passed it to the data property of the Vue instance. We then split it in a computed property, converting the nested data items into a single data item. Next, we use the v-for directive to loop through the new array and render it as an HTML table.
Summary
In Vue, it is very simple to use the v-for instruction to loop out data. You just need to follow the basic syntax, and armed with the right data, you can start building complex templates. Whether you're iterating over simple arrays or more complex nested data structures, the v-for directive can help you handle them.
The above is the detailed content of How to use v-for instruction to loop output data in Vue. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.
