vue基础:父组件主动获取子组件的数据和方法

一.vue基础:父组件主动获取子组件的数据和方法

1.调用子组件的时候定义一个ref
在Home.vue

<v-header ref='header'/>

2.在父组件里面里面通过
this. r e f s . h e a d e r . t h i s . refs.header.属性 this. refs.header.方法

getchild(){
            alert(this.$refs.header.msg)
        }

父组件通过调用getchile(){}方法就可以获取子组件的数据和方法。

二.子组件主动获取父组件的数据和方法

因为已经定义了父子组件所以直接在子组件中获取就行了。

this.$parent.数据
this.$parent.方法
发布了51 篇原创文章 · 获赞 45 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/a1424261303/article/details/86589523