Example code of looping using v-for instruction
This article mainly introduces you to the relevant information about the common instructions of Vue.js, the loop use of v-for instructions. The article introduces it in detail through the example code, which has certain reference and learning value for everyone. Friends who need it Let’s take a look below.
Preface
In Vue.js, the v-for directive requires special syntax in the form of item in items, items is the source dataArrayAnd item is an alias for array element iteration.
v-for can bind data to an array to render a list:
<p id="wantuizhijia"> <ol> <li v-for="site in sites"> {{ site.name }} </li> </ol> </p> <script> new Vue({ el: '#wantuizhijia', data: { sites: [ { name: '网推之家' }, { name: '谷歌' }, { name: '淘宝' } ] } }) </script>
Output:
Use v-for in template:
<p id="wantuizhijia"> <ul> <template v-for="place in places"> <li>{{ place.name }}</li> <li>--------------</li> </template> </ul> </p> <script> new Vue({ el: '#wantuizhijia', data: { places: [ { name: '厦门' }, { name: '漳州' }, { name: '福州' } ] } }) </script>
Effect:
##v-for can pass the attribute of an object To iterate the data:
##
<p id="wangtuizhijia"> <ul> <li v-for="value in object"> {{ value }} </li> </ul> </p> <script> new Vue({ el: '#wangtuizhijia', data: { object: { name: '脚本之家', url: 'www.jb51.net', slogan: '美好生活,等待你的开创!' } } }) </script>
Effect:
Script Home
www.jb51.net
A wonderful life is waiting for you to create it!
#v-for iterates data through the properties of an object. You can provide the second parameter as the key name:
<p id="wangtuizhijia"> <ul> <li v-for="(value, key) in object"> {{ key }} : {{ value }} </li> </ul> </p> <script> new Vue({ el: '#wangtuizhijia', data: { object: { name: '脚本之家', url: 'www.jb51.net', slogan: '美好生活,等待你的开创!' } } }) </script>
Effect:
name: Script House
url: www.jb51.net
slogan: A better life is waiting for you to create it!
#v-for Iterates data through the properties of an object, taking the third parameter as the index :
<p id="wangtuizhijia"> <ul> <li v-for="(value, key, index) in object"> {{ index }} {{ key }}:{{ value }} </li> </ul> </p> <script> new Vue({ el: '#wangtuizhijia', data: { object: { name: '脚本之家', url: 'www.jb51.net', slogan: '美好生活,等待你的开创!' } } }) </script>
Effect:
0 name:Script Home
1 url:www.jb51.net
2 slogan: A better life is waiting for you to create it!
<p id=”wangtuizhijia”> <ul> <li v-for=”n in 10″> {{ n }} </li> </ul> </p> <script> new Vue({ el: ‘#wangtuizhijia' }) </script> </body>
Effect:
1
23
4
5
6
7
8
9
10
The above is the detailed content of Example code of looping using v-for instruction. 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

AI Hentai Generator
Generate AI Hentai for free.

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



How to get items using commands in Terraria? 1. What is the command to give items in Terraria? In the Terraria game, giving command to items is a very practical function. Through this command, players can directly obtain the items they need without having to fight monsters or teleport to a certain location. This can greatly save time, improve the efficiency of the game, and allow players to focus more on exploring and building the world. Overall, this feature makes the gaming experience smoother and more enjoyable. 2. How to use Terraria to give item commands 1. Open the game and enter the game interface. 2. Press the "Enter" key on the keyboard to open the chat window. 3. Enter the command format in the chat window: "/give[player name][item ID][item quantity]".

Support Vector Machine (SVM) in Python is a powerful supervised learning algorithm that can be used to solve classification and regression problems. SVM performs well when dealing with high-dimensional data and non-linear problems, and is widely used in data mining, image classification, text classification, bioinformatics and other fields. In this article, we will introduce an example of using SVM for classification in Python. We will use the SVM model from the scikit-learn library

This article aims to help beginners quickly get started with Vue.js3 and achieve a simple tab switching effect. Vue.js is a popular JavaScript framework that can be used to build reusable components, easily manage the state of your application, and handle user interface interactions. Vue.js3 is the latest version of the framework. Compared with previous versions, it has undergone major changes, but the basic principles have not changed. In this article, we will use Vue.js instructions to implement the tab switching effect, with the purpose of making readers familiar with Vue.js

With the continuous advancement of digital technology, front-end development has become an increasingly popular profession. Vue3 has become the first choice of more and more developers due to its simplicity, ease of use, efficiency and stability. Among them, the v-for function is one of the core functions used for list data rendering in Vue3. In this article, we will provide a detailed explanation of the v-for function in Vue3, so that you can better use it to solve actual development problems. 1. Basic syntax The basic syntax of the v-for function is as follows: <divv-

Vue is a modern JavaScript framework that helps us build dynamic web pages and complex applications easily. In Vue, you can easily create loop structures using v-for to iteratively render data. In some specific scenarios, we can also use v-for to implement dynamic sorting. This article will introduce how to use v-for to implement dynamic sorting techniques in Vue, as well as some application scenarios and examples. 1. Use v-for to make it simple

Best practices and performance optimization methods for using v-for in Vue Introduction: In Vue development, it is very common to use the v-for instruction, which can conveniently traverse and render the data of an array or object onto the template. However, improper use of v-for may cause performance issues when dealing with large-scale data. This article will introduce the best practices when using the v-for directive and provide some performance optimization methods. Best practice: When using the v-for instruction in Vue to render each item of data using a unique Key value, you need to

As the new generation of front-end frameworks continues to emerge, VUE3 is loved as a fast, flexible, and easy-to-use front-end framework. Next, let's learn the basics of VUE3 and make a simple video player. 1. Install VUE3 First, we need to install VUE3 locally. Open the command line tool and execute the following command: npminstallvue@next Then, create a new HTML file and introduce VUE3: <!doctypehtml>
![How to solve '[Vue warn]: v-for='item in items': item' error](https://img.php.cn/upload/article/000/000/164/169241709258603.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
How to solve the "[Vuewarn]:v-for="iteminiitems":item" error During the Vue development process, using the v-for directive for list rendering is a very common requirement. However, sometimes we may encounter an error: "[Vuewarn]:v-for="iteminiitems":item". This article will introduce the cause and solution of this error, and give some code examples. First, let’s understand
