A
A
Artem Nanavov2019-12-13 09:40:46
Vue.js
Artem Nanavov, 2019-12-13 09:40:46

How to pass input value to parent?

I have an array, and I want the input value to be added to the array when the button is pressed, but it should be entered undef
here is the code
//===================== ========================================
parent component

<template>
    <div id="app" class="container">
      <h1> {{ title }} </h1>
        <TodoInput :items="items" @push="pushForItem" ></TodoInput>
        <TodoList></TodoList>
    </div>
</template>

<script>

    import TodoInput from "./components/TodoInput";
    import TodoList from "./components/TodoList";

    export default {
      props: {
        value: String
      },

      data() {
        return {
          title: "TodoList",
          items: [],
        }
      },
      components: {
          TodoInput,
          TodoList
      },
      methods: {
          pushForItem() {
              var input = this.value;

              if ( this.value !== "" ) {
                  this.items.push({
                      text:input
                  })
              }
          }
      }
    }

</script>

//=============================================== ========================
//======================== ==============================================
child component
<template>
    <div>
        <input type="text" v-model="value">
        <button class="btn btn-outline-secondary" @click="addItem">Add</button>
    </div>
</template>

<script>

    export default {
        props: {
            items: {
                type: Array
            },
            onValue: {
                type: String
            }
        },
        data() {
            return {
                value: ""
            }
        },

        methods: {
            addItem() {
                this.$emit("push", this.value);
            }
        }
    }

</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Barkowski, 2019-12-14
@TypeOFF

I advise the author of the post to re-read the meaning of props and work with events coming from child components.
The answer above solves the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question