vue获取子组件数据的三个方法

Posted on 2019-10-18 18:58  猫头唔食鱼  阅读(26157)  评论(0编辑  收藏  举报

1.this.$emit ,子传父

2.this.$children属性

this.$children返回的是数组

例子:

//获取子组件数据
       console.log(this.$children[0].cdata);
 //调用子组件方法
    this.$children[0].cmethod()

 

3.通过this.$refs获取组件

 //获取子组件数据
       console.log(this.$refs.test.cdata);
//调用子组件方法
    this.$refs.test.cmethod()

 

4.this.$parent获取父组件数据

this.$parent返回的是对象,this.$children返回的数组

例子:

 console.log(this.$parent.pdata);