使用Vue JS在v-repeat中定位最后一项

nol*_*and 4 javascript vue.js

我有一个从json中提取的项目的v重复列表.

我想定位列表中的最后一项来更改其类.

我该怎么做?我的代码如下......

HTML

<div id="app">
...
            <ul id="menu">
                <li v-repeat="items">
                    <i class="material-icons">{{ icon }}</i>
                    {{ name }}
                </li>
            </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

JS

var apiUrl = 'inc/menu.json.php'

new Vue({
    el: '#app',
    data: {
        active: true,
        items: []
    },
    ready: function(){
        this.fetchData()
    },
    methods: {
        toggle: function () {
            this.active = !this.active;
        },
        fetchData: function(){
            var xhr = new XMLHttpRequest(),
                self = this
            xhr.open('GET', apiUrl)
            xhr.onload = function () {
                self.items = JSON.parse(xhr.responseText)
            }
            xhr.send()
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

Mar*_*mro 15

对于Vue 2.0,代码看起来略有不同:

<li v-for="(item, index) in items" v-bind:class="{last : index === (items.length-1)}">
  <i class="material-icons">{{ icon }}</i>
    {{ name }}
</li>
Run Code Online (Sandbox Code Playgroud)


Jih*_*ada 5

你只需要像这样添加 v-class

<li v-repeat="items" v-class="last : $index === (items.length-1)">
  <i class="material-icons">{{ icon }}</i>
    {{ name }}
</li>
Run Code Online (Sandbox Code Playgroud)

last您要添加的班级在哪里