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