如何从 Vue 中的父级访问子组件数据? [英] How can I access child component data from a parent in Vue?

查看:27
本文介绍了如何从 Vue 中的父级访问子组件数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 Vue 中的单文件组件访问数据,但找不到任何方法.我曾尝试使用 $emit,但也无法使用.数据字符串必须为空,因为它会被输入字段修改.

I am trying to access data from a single-file component in Vue, but can't find any way of doing it. I have tried using $emit, but can't get thath to work either. The data string has to be blank as it gets modified by an input field.

我在 SO 上看到了其他人的解决方案,但要么不适合我的问题,要么我无法让他们工作.我想尽量让它简单易懂.

I have seen others' solutions here on SO, but either the don't fit with my problem or I can't get them to work. I want to try to keep it as simple and understandable as possible.

推荐答案

您可以使用特殊属性 ref:

You can use the special attribute ref:

<child-comp ref="child"></child-comp>

在JS中:

vm.$refs.child.YOUR_DATA

希望这会有所帮助!

这篇关于如何从 Vue 中的父级访问子组件数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆