vue.js-方法无法遍历数组属性

时间:2018-11-12 20:17:41

标签: vuejs2

我的应用通过axios从api读取json数据数组。 我将数组存储在app属性中。一切都很好,我可以检查一下是否有阵列数据。

当方法应遍历数组时,会显示错误消息:

[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这个数组,看起来很完美。 预先感谢您的反馈!

1 个答案:

答案 0 :(得分:0)

很显然,# read your data from clipboard df = pd.read_clipboard() # run your pivot_table code from above report = df.groupby(['SUPER_TYPE']).apply(lambda sub_df: sub_df.pivot_table(index=['STRATA', 'OS_TYPE', 'STAND_NUMB', 'SILV_PRES'], values=['ACRES'],aggfunc=np.sum, margins=True,margins_name= 'TOTAL')) # this is creating a new row at level(1) called grand total # set it equal to the sum of ACRES where level(1) != 'Total' so you are not counting the calculated totals in the total sum report.loc[('', 'Grand Total','','',''), :] = report[report.index.get_level_values(1) != 'TOTAL'].sum() report ACRES SUPER_TYPE STRATA OS_TYPE STAND_NUMB SILV_PRES HS HS3B HS3B 3092.0 OSR/2SS/SCC 17.3 3580.0 OSR/2SS/SCC 8.1 3581.0 OSR/2SS/SCC 16.6 3587.0 OSR/2SS/SCC 13.8 3594.0 OSR/2SS/SCC 31.7 3607.0 OSR/2SS/SCC 27.7 TOTAL 115.2 HW H3AB H3A 3571.0 OSR/2SS/SCC 30.7 3573.0 OSR/2SS/SCC 30.4 3585.0 OSR/2SS/SCC 25.8 3588.0 OSR/2SS/SCC 18.1 3589.0 OSR/2SS/SCC 54.7 3597.0 OSR/2SS/SCC 41.6 3601.0 OSR/2SS/SCC 11.9 . . . Grand Total 813.6 无法在v-for上进行操作,因为null可迭代的

如果您尝试在最初为null的属性上使用v-for,直到后来异步变为null,则有两个选择:

  1. 初始化Array而不是diskstatus: []
  2. 在您要尝试null的元素上,添加防护:v-for