「Vue.js」v-forディレクティブの使い方
環境
vue2.6.14
v-forの書式
<div v-for="(item, index) in items"></div>
配列、オブジェクトのプロパティなどのリスト操作に利用します。
key属性 を指定すべきです。
使用例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 最新IT技術情報_arkgame.com</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ol>
<li v-for="cft in sites">
{{ cft.name }}
</li>
</ol>
</div>
<script>
new Vue({
el: '#app',
data: {
sites: [
{ name: 'arkgame' },
{ name: 'google' },
{ name: 'yahoo' },
{ name: 'Facebook' }
]
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 最新IT技術情報_arkgame.com</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ol>
<li v-for="cft in sites">
{{ cft.name }}
</li>
</ol>
</div>
<script>
new Vue({
el: '#app',
data: {
sites: [
{ name: 'arkgame' },
{ name: 'google' },
{ name: 'yahoo' },
{ name: 'Facebook' }
]
}
})
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 最新IT技術情報_arkgame.com</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script> </head> <body> <div id="app"> <ol> <li v-for="cft in sites"> {{ cft.name }} </li> </ol> </div> <script> new Vue({ el: '#app', data: { sites: [ { name: 'arkgame' }, { name: 'google' }, { name: 'yahoo' }, { name: 'Facebook' } ] } }) </script> </body> </html>
実行結果
1.arkgame
2.google
3.yahoo
4.Facebook