0

When I put the input field inside a component, it doesn't update the value by any means. Is there something I've missed? Btw, I use Vue 3.

Child Component (input-form.vue)

    <template>
      <input
        type="text"
        :value="searchField"
        @input="$emit('update:searchField', $event.target.value)"
      />
      <p>Text Inside: {{ searchField }}</p>
    </template>
    
    <script>
    export default {
      emits: ["update:searchField"],
      props: {
        searchField: {
          type: String,
        },
      },
    };
    </script>

Parent

    <template>
      <div>
        <input-form :searchField="searchField" />
        <p>Text Outside: {{ searchField }}</p>
      </div>
    </template>
    
    <script>
    import inputForm from "components/input-form.vue";
    export default {
      data() {
        return {
          searchField: "",
        };
      },
      components: {
        inputForm,
      },
    };
    </script>
2
  • Hey mate :), what do you want to achieve with input? Commented Aug 27, 2021 at 8:49
  • I want to get the data I type on the input field and pass it along from the child component to the parent. I made the input field a component in order to use it on other pages with the same style. Commented Aug 27, 2021 at 9:04

1 Answer 1

3

You are not listening for the update:searchField event in the Parent.

<input-form :searchField="searchField" @update:searchField="searchField = $event" />

or

<input-form v-model:searchField="searchField" />
Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thanks a lot. I forgot to add the listener.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.