在我的阵列上使用axios发送数据的问题

时间:2019-10-09 09:18:43

标签: javascript arrays vue.js axios

我已将数据和轴位发送给数组(mySplitDays [splitDayIndex] = splitDay),但我有一个错误...

components: { VueCal },
data() {
    return {
        users:[],
        mois:[],
        assignments:[],
        selectedEvent: {},
        showDialog: false,
        events: [],//recuperer tache ici
        minCellWidth: 400,
        minSplitWidth: 150,
        splitDays:mySplitDays        
    };
},
async mounted() {
    let response = await axios
    .get(`${process.env.***}/users?role=***&active=***`)
    let mySplitDays = response.data
    for (let splitDayIndex in mySplitDays){
        let splitDay= mySplitDays[splitDayIndex]
        splitDay.class = splitDay.lastname
        splitDay.label = splitDay.lastname
        mySplitDays[splitDayIndex]=splitDay
    }
}

[Vue警告]:data()中的错误:“ ReferenceError:未定义mySplitDays”

ReferenceError:未定义mySplitDays

[Vue警告]:渲染错误:“ TypeError:无法读取未定义的属性'length'”

TypeError:无法读取未定义的属性“ length”     在Proxy.render上(评估在./node_modules/vue-loader/lib/template-compiler/index.js?{“ id”:“ data-v-aae30ed8”,“ hasScoped”:true,“ transformToRequire”:{“ video“:[” src“,” poster“],” source“:” src“,” img“:” src“,” image“:” xlink:href“},” buble“:{” transforms“:{ }}}!../ node_modules / vue-loader / lib / selector.js?type = template&index = 0!./ src / components / Dashboard.vue(app.js:2705):: 15:31)

1 个答案:

答案 0 :(得分:1)

您正在尝试在创建组件时将mySplitDays分配给数据。那时没有变量称为存在。您应该在splitDays中添加一个默认值,例如一个空数组。空数组可能是最好的默认值,因为它具有length属性,因此,如果在模板中的某处使用它,则不会导致问题。然后在已安装的函数内部,将axios的结果分配给数据。