0

What's the problem

I wanted to assign a local component variable to prop. I constantly get Vue alert Invalid watch handler specified by key "undefined". Maybe the case is that the prop is passed from another component, where I use v-model, but I don't really know. I would really appreciate your help, because my small exercise project really depends on this mechanic.

Parent Component

Here I have some HTML select, this is where I actually model my state.selectedPhysicsModule

<template>
  <div>
    <div>
      <h1>Some header</h1>
    </div>
    <form class="choosePhysicsModule">
      <label for="selectedPhysicsModule"></label>
      <select class="select_Module" id="selectedPhysicsModule" v-model="state.selectedPhysicsModule">
        <option :value="option.value" v-for="(option, index) in importedListToSelect" :key="index">
          {{option.name}}
        </option>
      </select>
    </form>
    <list_of_exercises  v-if="state.selectedPhysicsModule" :what_exercises="state.selectedPhysicsModule"/>
  </div>
</template>

<script>

export default {
  name: 'ChoosePhysicsModule',
  components: {list_of_exercises},
  setup() {
    const state = reactive({
      selectedPhysicsModule: null,
    })
    return {
      state,
      importedListToSelect
    }
  }
}

Child Component

</script>

export default {
    name: "list_of_exercises",
      props: {
        whatExercises: {
          type: String,
          required: true
        }
      },
      data() {
       return {
         exercises: this.what_exercises,
       }
      },
      watch: {
        whatExercises: function () {
          this.exercises = this.whatExercises
        }
       }
4
  • You have circular dependency in the watch. Please attach your parent component also so we can help you better. Commented Dec 8, 2020 at 16:03
  • 2
    maybe try adding default: '' to the prop @BallonUra no OP is mutating exercises Commented Dec 8, 2020 at 16:04
  • I made some changes, maybe with this broader context you will be able to help me? Commented Dec 9, 2020 at 6:36
  • Do you have some ideas what can I do with this alert? Please help me Commented Dec 12, 2020 at 20:08

1 Answer 1

1

In the parent component where you are passing the prop you need to add a setter for the prop passed. Here is an example:

<template>
  <div id="app">
      <label>
          <input name="whatExercises" v-model="whatExercises">
      </label>

      <ListOfExercises v-if="whatExercises" :what_exercises="whatExercises" />
  </div>
</template>

<script>
export default {
    data() {
      return {
          whatExercises: null,
      }
    }
}
</script>

P.S: as a side note, I recommend using camelCase for prop names. It's more in-line with the rest of the community. If you have time feel free to check out the style guide on the official website.

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm, I still get this alert, I made some changes in my post, maybe this will give us broader context on the situation. And of course thank you for sharing this information about community standards.
so what should I do?
Sorry I couldn't respond earlier. Is you problem already fixed?
Yes, it is. I just changed watch to computed and changed my code a little bit. Maybe you didn't help me directly, but thanks to you I understood what was wrong. Thank you.
Would be better if you showed your actual solution, otherwise this post is useless to anyone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.