访问属性时,从子对象到父对象的发射对象未定义

时间:2019-12-05 12:43:08

标签: javascript vue.js events vuejs2

我已经实现了以下定义的简单自定义事件。但是,每当我将值发送回父级时,访问其属性都将导致未定义。我完全不知道为什么...

<!-- 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!
}

/

<-- 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 
  );
}

更新

我使用dubugger获取当前状态并将返回类型设置为任何以防万一。 enter image description here

0 个答案:

没有答案