Javascript 访问属性时,未定义从子对象发射到父对象的对象
我已经实现了下面定义的一个简单的自定义事件。但是,每当我得到返回父级的值时,访问其属性就会得到未定义的值。我不知道为什么Javascript 访问属性时,未定义从子对象发射到父对象的对象,javascript,vue.js,events,vuejs2,Javascript,Vue.js,Events,Vuejs2,我已经实现了下面定义的一个简单的自定义事件。但是,每当我得到返回父级的值时,访问其属性就会得到未定义的值。我不知道为什么 <!-- Parent Component function --> onAddExercise( workoutIndex: number, workoutRoundIndex: number, exercise: ExerciseModel ) { console.log(exercise); outputs the whole object
<!-- Parent Component function -->
onAddExercise(
workoutIndex: number,
workoutRoundIndex: number,
exercise: ExerciseModel
) {
console.log(exercise); outputs the whole object with values
console.log(exercise.id); undefined!
console.log(exercise.name); undefined!
}
不必要的运动(
工作索引:编号,
workoutRoundIndex:number,
练习:练习模型
) {
log(练习);使用值输出整个对象
console.log(exercise.id);未定义!
console.log(exercise.name);未定义!
}
/
/
异步onSelectExercise(exerciseId:string){
var response=等待此消息。$apollo.query({
问题:演习,,
变量:{
id:exerciseId
}
});
让exercise=response.data作为ExerciseModel
log(练习);使用值输出整个对象
console.log(exercise.id);输出id
console.log(exercise.name);输出名称
这是$emit(
“onAddExercise”,
这个.workoutIndex,
这个.workoutRoundIndex,
运动
);
}
更新
我使用dubugger获取当前状态,并将返回类型设置为any以防万一。
是否正在将类型设置为
ExerciseModel
此处执行意外操作?假设console.log(exercise)
的输出类似于{id:'123',name:'test'}
@ebbishop Check updated question:),那么调试器的屏幕截图显示变量exercise
包含属性为exercise
的对象。然而,不清楚这是怎么发生的,因为你发布的代码看起来还不错……你能提供一个,或者类似的东西吗?
<-- Render Child Component in Parent -->
<ExerciseSearch @onAddExercise="onAddExercise"
:workoutIndex="wIndex"
:workoutRoundIndex="wrIndex"
/>
<-- Child Component function -->
async onSelectExercise(exerciseId: string) {
var response = await this.$apollo.query({
query: EXERCISE,
variables: {
id: exerciseId
}
});
let exercise = response.data as ExerciseModel
console.log(exercise); outputs the whole object with values
console.log(exercise.id); outputs the id
console.log(exercise.name); outputs the name
<!-- Emit to Parent with payload -->
this.$emit(
"onAddExercise",
this.workoutIndex,
this.workoutRoundIndex,
exercise
);
}