Help us understand the problem. What is going on with this article?

【Vuejs】v-forで配列を出力する時に1つ目の要素とそれ以降でstyleを変える

テーブルとかもそうですが、最初の要素だけスタイルを切り替えたい時にどうするんだろうと調べていたら3項演算子が使えました。

class属性を配列の要素番号で切り替える

sample.html
    <style>
        .first{
            color: aquamarine;
        }
        .two-or-more{
            color: burlywood;
        }
    </style>
    <div id="app">
        <ul>
            <li v-for="(e,index) in mylist" v-bind:class="[index == 0 ? 'first' : 'two-or-more']">{{e.name}}</li>
        </ul>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                mylist: [
                    { name: "1つ目" },
                    { name: "2つ目" },
                    { name: "3つ目" },
                    { name: "4つ目" },
                ],
            }
        })
    </script>

結果

画面はこんな感じで、
Screenshot2020-02-16_20-09-52.png

要素はこんな感じで出力されました。
Screenshot2020-02-16_20-13-42.png

参考

公式ドキュメント#配列構文
https://jp.vuejs.org/v2/guide/class-and-style.html#%E9%85%8D%E5%88%97%E6%A7%8B%E6%96%87

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした