3

Among the approaches below, which do you think is better practice ??

[ 1 ] Using $emit to expose methods from child component to parent component

$emit('updateAPI',  exposeAPI({ childMethod: this.childMethod }))

OR

[ 2 ] Using $refs from parent component to access child component methods

this.$refs.childComponent.childMethod() 
2
  • You should read the answer over here.Everything is explained really well for understanding. stackoverflow.com/questions/40957008/… Commented Apr 12, 2019 at 8:38
  • Emit - definitely. It completely decouples the parent and child element to increase reusability. Commented Apr 12, 2019 at 11:59

1 Answer 1

1

About $refs, according to the docs:

"$refs are only populated after the component has been rendered, and they are not reactive. It is only meant as an escape hatch for direct child manipulation - you should avoid accessing $refs from within templates or computed properties."

About callbacks, I have no information about cons and there is a nice example in script section of this component of Quasar Framework, which parent component recieves via emit a function called reset and can dispatch this child function. That's why I think this way is preferable.

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

1 Comment

yes.. thank you.. i got it.... BTW so far i'm using `` $refs ``only for call focus();

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.