2

I'm new to VUE and am trying to clean up my code by creating components instead of repeating code.

How do you return a varform a child component?

Main.vue

<template>
     <square value="5" @click="result"></square>
</template>

Square.vue

[...]
mounted() {
     return { fromSquare: value*value } 
}

Main.vue

methods: {
   result(fromSquare) {
       this.squaredResult = fromSquare;
   }

}

1 Answer 1

3

You've to emit an event from square component with value*value as payload and add its handler in parent :

mounted() {
    this.$emit("emit-result",value*value)
}

Main.vue

<template>
     <square value="5" @click="result" @emit-result="result"></square>
</template>

....

methods: {
   result(fromSquare) {
       this.squaredResult = fromSquare;
   }

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

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.