父組件主動獲取子組件的數據和方法:
1.調用子組件的時候定義一個ref
<v-header ref="header"></v-header>
2.在父組件里面通過
this.$refs.header.屬性
this.$refs.header.方法
<template>
<!-- 所有的內容要被根節點包含起來 -->
<div id="home">
<v-header ref="header"></v-header>
<hr>
首頁組件
<button @click="getChildData()">獲取子組件的數據和方法</button>
</div>
</template>
-------------------------------------------------------------------
methods:{
run(){
alert('我是Home組件的run方法');
},
getChildData(){
//父組件主動獲取子組件的數據和方法:
// alert(this.$refs.header.msg);//數據
this.$refs.header.run();//方法
}
}
