0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue.js v-forの使い方 配列とオブジェクト比較

Last updated at Posted at 2020-07-15

前回のv-forはこちら
Vue.js v-forの使い方

配列とオブジェクトで指定の仕方に違いがあった為その実装方法の備忘録

コード


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Vueの練習:v-for(基本的な配列とオブジェクトの比較</h1>


<!-- 基本の配列のv-for -->
<div id="app">
    <ul>
        <li v-for='item in items'>{{item}}</li>
    </ul>
</div>

<!-- オブジェクトの配列のv-for -->
<div id="app2">
    <ul>
        <li v-for='item in items'>{{item.name}}</li>
    </ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>

// 基本の配列のv-for
new Vue({
    el:'#app',
    data:{
        items:['北海道', '青森県', '岩手県' ,'宮城県' ,'秋田県' ,'山形県']
    }
})

// オブジェクトの配列のv-for
new Vue({
    el:'#app2',
    data:{
        items:[
            {name:'北海道'},
            {name:'青森県'},
            {name:'岩手県'},
            {name:'宮城県'},
            {name:'秋田県'},
            {name:'山形県'}
            ]
    }
})

    
</script>
</body>
</html>

結果
スクリーンショット 2020-07-16 6.20.10.png

ポイント

html側

マスタッシュの書き方
通常の配列:{{item}}
オブジェクト:{{item.name}}

js側

通常の配列:

 data:{
        items:['北海道', '青森県', '岩手県' ,'宮城県' ,'秋田県' ,'山形県']
    }

オブジェクト:

data:{
        items:[
            {name:'北海道'},
            {name:'青森県'},
            {name:'岩手県'},
            {name:'宮城県'},
            {name:'秋田県'},
            {name:'山形県'}
            ]
      }
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?