【发布时间】:2018-11-12 20:17:41
【问题描述】:
我的应用通过 axios 从 api 读取 json 数据数组。 我将数组存储在应用程序属性中。一切都很好,我可以检查并查看数组数据。
当一个方法应该遍历数组时,它会给出一个错误消息:
[Vue warn]: Error in render: "TypeError: Cannot read property 'length' of null"
这是我的应用程序中的代码:
"use strict";
const app = new Vue({
el: '#app',
data: {
diskstatus: null,
},
methods: {
hasServer: function (serverName) {
for (let i = 0; i < this.diskstatus.length; i++) {
if (this.diskstatus[i].ServerName == serverName) {
return 'Y';
}
}
return 'N';
}
},
mounted() {
axios
.get('api/diskstatus')
.then(response => {
this.diskstatus = response.data.rows;
})
.catch(err => {
console.log(err)
});
}
});
知道为什么方法中的代码没有将属性视为数组吗?
我可以 console.log 数组,它看起来很完美。 提前感谢您的反馈!
【问题讨论】:
-
diskstatus最初设置为null。也许hasServer调用是在加载统计数据之前发生的? -
显示您尝试
v-for的HTML。 -
显示善意回报。
标签: vuejs2