[Vuejs]-Delivery data from parent to child

0👍

you have a spelling error on return (retrun), and there could be other errors on code you have omitted.

Working live demo

Here it is working on a live demo

Code

Parent

<template>
  <div class="parent">
    <child :myprop="dataTochild" />
    <button @click="callToChild">Press me!</button>
  </div>
</template>

<script>
import Child from "./Child.vue";

export default {
  name: "Parent",
  data() {
    return {
      dataTochild: "",
      counter: 0
    };
  },
  methods: {
    callToChild: function() {
      this.counter = this.counter + 1;
      this.dataTochild = "Hello Child " + this.counter;
    }
  },
  components: {
    Child
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
button {
  width: 200px;
  height: 60px;
}
</style>

Child

<template>
  <div class="child">
    <p>{{ myprop }}</p>
  </div>
</template>

<script>
export default {
  name: "Child",
  props: ["myprop"],
  watch: {
    myprop: function() {
      console.log("Hello Parent");
      alert("Hello Parent");
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
div.child {
  width: 400px;
  height: 100px;
  margin: auto;
}
</style>

Leave a comment