Home Web Front-end JS Tutorial Example code of looping using v-for instruction

Example code of looping using v-for instruction

Jun 29, 2017 pm 03:51 PM
v-for Example 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: &#39;#wantuizhijia&#39;,
  data: {
   sites: [
    { name: &#39;网推之家&#39; },
    { name: &#39;谷歌&#39; },
    { name: &#39;淘宝&#39; }
   ]
  }
 })
</script>
Copy after login

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: &#39;#wantuizhijia&#39;,
  data: {
   places: [
    { name: &#39;厦门&#39; },
    { name: &#39;漳州&#39; },
    { name: &#39;福州&#39; }
   ]
  }
 })
</script>
Copy after login

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: &#39;#wangtuizhijia&#39;,
  data: {
   object: {
    name: &#39;脚本之家&#39;,
    url: &#39;www.jb51.net&#39;,
    slogan: &#39;美好生活,等待你的开创!&#39;
   }
  }
 })
</script>
Copy after login

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: &#39;#wangtuizhijia&#39;,
  data: {
   object: {
    name: &#39;脚本之家&#39;,
    url: &#39;www.jb51.net&#39;,
    slogan: &#39;美好生活,等待你的开创!&#39;
   }
  }
 })
</script>
Copy after login

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: &#39;#wangtuizhijia&#39;,
  data: {
   object: {
    name: &#39;脚本之家&#39;,
    url: &#39;www.jb51.net&#39;,
    slogan: &#39;美好生活,等待你的开创!&#39;
   }
  }
 })
</script>
Copy after login

Effect:

0 name:Script Home


1 url:www.jb51.net


2 slogan: A better life is waiting for you to create it!


v-for can also loop integer

<p id=”wangtuizhijia”>
<ul>
<li v-for=”n in 10″>
{{ n }}
</li>
</ul>
</p>
<script>
new Vue({
el: ‘#wangtuizhijia&#39;
})
</script>
</body>
Copy after login

Effect:

1

2

3
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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get items using commands in Terraria? -How to collect items in Terraria? How to get items using commands in Terraria? -How to collect items in Terraria? Mar 19, 2024 am 08:13 AM

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 &quot;Enter&quot; key on the keyboard to open the chat window. 3. Enter the command format in the chat window: &quot;/give[player name][item ID][item quantity]&quot;.

SVM examples in Python SVM examples in Python Jun 11, 2023 pm 08:42 PM

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

VUE3 quick start: using Vue.js instructions to switch tabs VUE3 quick start: using Vue.js instructions to switch tabs Jun 15, 2023 pm 11:45 PM

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

Detailed explanation of v-for function in Vue3: perfect solution to list data rendering Detailed explanation of v-for function in Vue3: perfect solution to list data rendering Jun 18, 2023 am 09:57 AM

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: &lt;divv-

Tips for using v-for to implement dynamic sorting in Vue Tips for using v-for to implement dynamic sorting in Vue Jun 25, 2023 am 09:18 AM

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 Best practices and performance optimization methods for using v-for in Vue Jul 17, 2023 am 08:53 AM

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

VUE3 Getting Started Example: Making a Simple Video Player VUE3 Getting Started Example: Making a Simple Video Player Jun 15, 2023 pm 09:42 PM

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: &lt;!doctypehtml&gt;

How to solve '[Vue warn]: v-for='item in items': item' error How to solve '[Vue warn]: v-for='item in items': item' error Aug 19, 2023 am 11:51 AM

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

See all articles