vue.js - 无法获取数组的索引

标签 vue.js html-table vuejs2

我试图在使用 v-for 时获取数组的当前索引,如下所示:

<tr v-for="(item,index) in timetable">
    Index: @{{index}}

    <td>@{{ item.subject }}</td>
    <div v-if="index == timetable.length - 1">
        <td>@{{ item.lesson_end }}</td>
    </div>
    <div v-else>
        <td>@{{ item.lesson_start }}</td>
    </div>
</tr>

但我只收到错误消息:

[Vue warn]: Property or method "index" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

为什么它声称索引未定义,根据文档这应该是有效的?

最佳答案

该问题完全是由于您的 HTML 结构无效造成的。 Vue 的 render 函数无法正确插入结果,因此您会收到该错误。

尝试类似的事情

<tr v-for="(item,index) in timetable">
  <td>Index: @{{ index }}</td>
  <td>@{{ item.subject }}</td>
  <td v-if="index == timetable.length - 1">@{{ item.lesson_end }}</td>
  <td v-else>@{{ item.lesson_start }}</td>
</tr>

http://jsfiddle.net/Lrvdjxpq/

关于vue.js - 无法获取数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52713948/

相关文章:

javascript - View |如何将Web端口号更改为80?

vue.js - VuexFire 突变

CSS Media Print - 使表格始终出现在单独的页面上

html - 电子邮件模板中未显示的部分

html - 在&lt;style&gt;和&lt;script&gt;vuejs中导入有什么区别?

vue.js - 如何在 vue tel 输入中获取国家/地区代码?

javascript - 异步函数未捕获( promise 中)未定义错误

javascript - 如何在使用 require.context 后动态加载 Vue 组件?

html - 如何使大表响应

javascript - Vue.js - 应该使用哪个组件生命周期来获取数据?