在以下方法中,您认为哪种方法更好??
[ 1 ] 使用$emit
将方法从子组件暴露给父组件
$emit('updateAPI', exposeAPI({ childMethod: this.childMethod }))
或
[ 2 ] 使用父组件的$refs
访问子组件方法
this.$refs.childComponent.childMethod()
最佳答案
关于$refs,根据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."
关于回调,我没有关于cons的信息,在this component的脚本部分有一个很好的例子。 Quasar Framework 的父组件通过发出一个名为 reset
的函数接收并可以分派(dispatch)此子函数。这就是为什么我认为这种方式更可取。
关于javascript - Vue 从子组件向父组件暴露一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647325/