gpt4 book ai didi

javascript - [Vue 警告] : Error in created hook: "TypeError: Cannot set property of undefined"

转载 作者:搜寻专家 更新时间:2023-10-30 22:34:25 30 4
gpt4 key购买 nike

我正在创建一个 VueJS 应用程序。我有一个子组件 Child.vue,数据从父组件传递到该组件。

Child.vue

export default{

props:['info'],

data: function(){
return{
timeValue:{
minutes: '',
hours: ''
}
}
},
created: function(){

console.log("Printing inside created of Child ",this.info);
this.convertMins(this.info[0][2]);

},

methods:{
convertMins: (minutes) => {
console.log("Printing inside convert mins", this);
if(minutes===0){
this.timeValue.minutes = 0;
this.timeValue.hours = 0;
}
if(minutes===60){
this.timeValue.hours = 1;
this.timeValue.minutes = 0;
}
if(minutes>60){
this.timeValue.hours = Math.floor(minutes/60);
this.timeValue.minutes = minutes % 60;
}
}
}

}

我的父组件是这样的,

Parent.vue

import Child from './Child.vue';

export default {
data(){
return{
info:[ ],

errors: []
}
},

created: function(){

this.accessInformation();
},

methods: {

accessInformation: function(){
axios.get(localhost:3000/retrieveInfo)
.then(response =>{
console.log(response.data.rows[3]);
this.info.push(response.data.rows[3]);
})
.catch(e => {
this.errors.push(e);
})
}
},

components: {
'child': Child,

}
}
<template>
<div>
<child v-bind:info="info" v-if="info.length > 0"></child>
</div>
</template>

当我尝试运行应用程序时,出现这样的错误,

Error

enter image description here

为什么我会收到这个错误?我是 VueJS 的新手。有人可以帮我解决这个问题吗?

最佳答案

不要使用箭头函数来定义方法。请参阅 https://v2.vuejs.org/v2/guide/instance.html#Data-and-Methods 处的警告框

关于javascript - [Vue 警告] : Error in created hook: "TypeError: Cannot set property of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47525044/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com