Vue父组件获取子组件

1.this.$refs

首先你的给子组件做标记demo :<firstchild ref="one"></firstchild>

然后在父组件中,通过this.$refs.one就可以访问了这个自组件了,包括访问自组件的data里面的数据,调用它的函数

//子组件
<Son ref="one"><Son/>//向子组件传递标记

//父组件
...
console.log(this.$refs.one)//父组件通过this.$refs.定义的标记名接收

2.this.$children

返回的是一个组件集合,如果知道子组件的顺序,可以使用下标来操作;也可以使用this.$children.$children...这种方式去获取

for(let i=0;i<this.$children.length;i++){
    console.log(this.$children[i].msg);输出子组件的msg数据;
}

猜你喜欢

转载自blog.csdn.net/weixin_44348028/article/details/106882207