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

标签 javascript vue.js

我有一个从 json 中提取的项目的 v-repeat 列表。

我想针对列表中的最后一项更改其类别。

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

HTML

<div id="app">
...
            <ul id="menu">
                <li v-repeat="items">
                    <i class="material-icons">{{ icon }}</i>
                    {{ name }}
                </li>
            </ul>
</div>

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()
        }
    }
});

最佳答案

对于 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>

关于javascript - 使用 Vue JS 定位 v-repeat 中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551979/

相关文章:

angularjs - 因为我现在正在使用 Laravel,所以我应该学习 Vuejs/Angular/React 吗?

javascript - 如何使 JavaScript indexOf() 不检测子字符串

javascript - Vuejs 从远程源获取图像

vue.js - 在组件和 Axios 中使用时如何使数据工作?

asp.net-core - 安装 vue N​​uGet 包后找不到任何要链接的 vue.js 文件

typescript - Pinia:无法从 Nuxt3 API 访问商店

javascript - 指令无法访问嵌入的元素?

javascript - 用变量解析 getJson

javascript - 如何将 jquery 应用于由 ajax 调用填充的标记

javascript - 从reducers渲染到React