4
2

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 5 years have passed since last update.

Vue.jsで繰り返し表示部分をコンポーネントに切り出す

Posted at

はじめに

以前の記事の繰り返し表示部分をコンポーネントとして切り出してみる。

コンポーネント

下記のjsを用意

item.js
var ItemComponent = Vue.extend({
    template:
        '<div class="item">' +
            '<div>ID: {{item.id}}</div>' +
            '<div>名前: {{item.name}}</div>' +
            '<div>値: {{item.value}}</div>' +
        '</div>',
    props: ['item']
})

コンポーネントの登録

index.jsでcomponentsを指定してコンポーネントを登録

index.js
var app = new Vue({
    el: '#app',
    data: {
        results: []
    },
    components: {
        'item-component': ItemComponent
    },
    mounted () {
        axios
            .get("api.php")
            .then(response => {this.results = response.data})
    }
});

HTMLでコンポーネントを使う

index.html
    <body>
        <div id="app">
            <div class="itemContainer">
                <item-component v-for="result in results" :key="result.id" v-bind:item="result"></item-component>
            </div>
        </div>
        <script src="item.js"></script>
        <script src="index.js"></script>
    </body>
4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?