0

I've got this code in my "ecedata" component with an input field:

<template>
    <div class="col-l-4">
        <p style="text-align: center">Data/day (GB)</p>
        <div class="form-input-set" style="background: white">
            <input v-bind:value="dataday" v-on:change="$emit('changedataday', $event.target.value)" type="number" class="form-input">
        </div>
    </div>
</template>

<script>
export default {
    name:"ecedata" ,
    props: {
        dataday:Number,
    },
    data () {
      return {

      }
    },

}
</script>

And I'm calling the component in the main app.vue with:

<template>
    <ecedata v-bind:dataday="dataday" v-on:changedataday="vuedataday" ></ecedata>
</template>

<script>
  import ecedata from '@/components/ece/data/ecedata.vue'

  export default {
    name: 'app',
    components: {
      ecedata,
    },
    data () {
      return {
          dataday:0,
      }
    },
    methods: {
      vuedataday() {
        alert(this.dataday)
      }
    },

  };

</script>

But it doesn't return a value on input in the form other than 0. Where's the error?

3 Answers 3

2

You just binded the value from the parent to the child component.

You where almost there as you binded the vuedataday method as a callback of the changedataday event this function is receiving the value passed by the event.

vuedataday(newDataday) {
    this.dataday = newDataday;
}

Another way to do two way binding is to use v-model.

<ecedata v-model="dataday"></ecedata>

And in the child

<input v-bind:value="dataday" v-on:change="$emit('input', $event.target.value)" type="number" class="form-input">

https://v2.vuejs.org/v2/guide/components.html#Using-v-model-on-Components

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

Comments

1

You defined state in your parent component, but never actually change it. The event on the other hand emits the changed value, but your onChange function never uses it.

You have to use the passed value and update the parents state.

vuedataday(value) {
  this.dataday = value;
  alert(this.dataday)
}

Comments

0

You should use @input event instead of @change event. In @input event you can call a function, inside function you can $emit parent method.

Your code should be write like this:

<template>
    <div class="col-l-4">
        <p style="text-align: center">Data/day (GB)</p>
        <div class="form-input-set" style="background: white">
            <input v-model="propsdata" @input="getValue" type="number" class="form-input">
        </div>
    </div>
</template>

<script>
export default {
    name:"ecedata" ,
    props: {
        dataday:Number,
    },
    data () {
      return {
         propsdata: this.dataday
      }
    },

    methods: {
       getValue() {
        this.$emit('changedataday', this.propsdata);
     }
   }
}
</script>

Your should use v-model in input binding and props variable to set another variable in child component. This style is good coding in vue.js. Thanks.

Comments

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.