Vue component 父访问子组件 $refs
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <div_box ref="box_1"></div_box> <div_box ref="box2"></div_box> <button @click="getChildComponent">btn_f</button> </div> <template id="div_box"> <div style="background-color: red;height: 200px;height: 200px;"> <button @click="btnClick">btn</button> </div> </template> <script src="js/vue.3.2.2.js"></script> <script> const Box = { data(){ return{ msg:"hello" } }, template:'#div_box', methods:{ btnClick() { console.log("btnClick"); } }, } // 1、创建Vue的实例对象 const app = Vue.createApp({ data(){//定义数据 return { msg:'你好!' } }, components:{ 'div_box':Box//, // 'div_box2':Box2, // 'div_box3':Box3 }, methods: { getChildComponent(){ console.log(this.$refs); console.log(this.$refs.box_1); console.log(this.$refs.box_1.msg); this.$refs.box_1.btnClick(); this.$refs.box2.btnClick(); } } }).mount('#app'); </script> </body> </html>