LoginSignup
0
0

Vue 基礎勉強 v-for

Posted at

(Vue.js 3.1.5を使用)

v-for

以下のように書くと、配列の要素をすべて表示することができる。

javascript
const app = Vue.createApp({
    data: () => ({
        animals: ['カラス', '', '', 'キツネ', 'たぬき']
    })
})
app.mount('#app')
html
    <div id="app">
        <ol>
            <li v-for="animal in animals">{{animal}}</li>
        </ol>        
    </div>

3.PNG

配列だけでなく、オブジェクトも扱える。

javascript
const app = Vue.createApp({
    data: () => ({
        book:{
            name: 'Vue 入門',
            author: 'やまだ花子',
            year: 2023,
            company: 'ほげほげ書房'
        },
    })
})
app.mount('#app')
html
    <div id="app">
        <ul>
            <li v-for="value in book">{{value}}</li>
        </ul>        
    </div>

4.PNG

htmlを以下のように書くと、各プロパティのvalueだけでなく、keyの値も表示できる。

html
    <div id="app">
        <ul>
            <li v-for="(value, key) in book">
                {{key}}: {{value}}
            </li>
        </ul>        
    </div>

5.PNG

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0