父组件 : <template> <div> <div class="name" ref="oneRef"> ref第一个 </div> <div class="name" ref="twoRef"> ref第2个 </div> <hello ref="ziRef"></hello> </div> </template> <script> import {ref,onMounted} from 'vue' import hello from './components/Hello.vue' export default { components:{ hello }, setup(){ const oneRef = ref() const twoRef = ref() const ziRef = ref() // 获取ref元素 onMounted(()=>{ console.log(oneRef.value) console.log(twoRef.value) // 获取子组件的实例 console.log(ziRef.value) // 调用子组件的实例的方法 console.log('调用子组件的方法 :' + ziRef.value.btn()) }) return { oneRef, twoRef, ziRef } } } </script>
子组件 : <template> <div>子组件</div> </template> <script> export default { setup(){ const btn = ()=>{ console.log('子组件的方法') } return { btn } } } </script> <style> </style>
标签:const,log,value,获取,组件,console,ref From: https://www.cnblogs.com/qd-lbxx/p/16619917.html