javascript - Vue.js - 从数组中获取 N 个元素 - 未定义

标签 javascript arrays vue.js

我的 Vue.JS 代码有一些问题。我需要获取数组的第 5 个元素,它工作正常,但 Vue 也显示一些错误

为了获取我的数据,我使用了这段代码:

<div>
  <span>{{ items[4].name }}</span>
</div>

它工作正常(Vue 显示数据)但我在控制台中也有这个错误:

[Vue warn]: Error in render: "TypeError: _vm.items[4] is undefined"

found in

---> <GeneralComponent> at resources/js/components/GeneralComponent.vue
       <General> at resources/js/views/General.vue
         <App> at resources/js/views/App.vue
           <Root>


TypeError: "_vm.items[4] is undefined"

最佳答案

我认为你的数组是在你的dom中访问它之后填充的,所以你可以这样试试:

{{ items[4] && items[4].name }}

关于javascript - Vue.js - 从数组中获取 N 个元素 - 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56417025/

相关文章:

javascript - JavaScript VM如何实现对象属性访问?是哈希表吗?

javascript - 如何让一段 javascript 只工作给定的次数?

c# - Streamreader 和 "Index was outside the bounds of the array"错误

c - 将未知大小的结构数组传递给函数并填充

javascript - 悬停时弹出特定元素

javascript - Vuex、vuejs - 未按预期对选择元素选项进行绑定(bind)

javascript - 在 JavaScript 中定义公共(public)字段的首选方式 : on prototype or constructor?

javascript - 为什么有些表单会在单击回车按钮时自动提交,而有些则不会?

python - 快速迭代多维 numpy 数组中的向量

javascript - VueJs 操作内联模板并重新初始化它