vue如何在父組件中去調(diào)用子組件的方法葬毫?
方法一:通過ref屬性
1.在父組件使用子組件時(shí)题篷,給子組件設(shè)置屬性ref值
例如:
<Child ref="child" ></Child>
2.在子組件中定義方法
例如:
getData() {
console.log("子組件中的getData方法");
}
3.在父組件中調(diào)用子組件中的方法
例如:
this.$refs.child.getData();
方法二:使用emit词身、on方法
$emit、$on
1.在子組件中定義方法
例如:
this.$on('getData', function() {
console.log("子組件中的getData方法");
});
2.在父組件中使用子組件中的方法
例如:
this.$refs.child.$emit("getData");