有没有更好的方法在 Vue 中使用 ref 访问嵌套子级? (祖父-父亲-儿子)
对于项目的结构方式,我无法改变,我需要从父亲一直到儿子的儿子(祖父与儿子的关系)获得一个价值。这是我所说的代码示例。
ComponentGrandfather.vue
<component-father
ref="refGF"
(...)
><component-father>
ComponentFather.vue
<component-son
ref="refF"
(...)
><component-son>
ComponentSon.vue
<input type=text ref="valueSon"></input>
我知道您可以使用 this.$ref.refGF 访问 Son。 $ref.refF.$ref.valueSon.value
,但我想知道是否有更简洁的方法,或者这是否是唯一的方法。
For how it is structured the project, and I cannot change, I need to get a value from a Father all the way down to the Son's son (Grandfather to Son relationship). This is a code example of what I am saying.
ComponentGrandfather.vue
<component-father
ref="refGF"
(...)
><component-father>
ComponentFather.vue
<component-son
ref="refF"
(...)
><component-son>
ComponentSon.vue
<input type=text ref="valueSon"></input>
I know that you can access to the Son using this.$ref.refGF.$ref.refF.$ref.valueSon.value
, but I want to know if there is a less verbose way to this, or if this is the only way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有这样做的道具。您可以将支柱从 GF 钻到父亲,然后再钻到儿子。
You have props for that. You can drill the prop from the GF to the Father and then to the Son.