直接上代码
在setup中使用绑定的ref
import { defineComponent, ref, onMounted } from 'vue' import { ElRadioGroup, ElRadio,RadioInstance } from 'element-plus' import CustomComponent from '@/components/CustomComponent' //自定义组件 export default defineComponent({ setup() { const chartRef = ref<HTMLDivElement | null>()//可以绑定为div或者任意组件 const radioRef = ref<RadioInstance>() //elradio的ref const customComponentoRef = ref<InstanceType<typeof CustomComponent>>() //自定义组件的ref 写法,通过ref.value.xxx调用子组件方式时,子组件中一定要把xxx这个方法return出来 onMounted(() => { console.log(chartRef.value) // Element div... }) return () => <div ref={chartRef} style="{{width: 200px, height: 100px}}"></div> }, })
在render 函数中使用
需要先在setup中return出来
import { defineComponent, ref, onMounted } from 'vue' import { ElRadioGroup, ElRadio,RadioInstance } from 'element-plus' import CustomComponent from '@/components/CustomComponent' //自定义组件 export default defineComponent({ setup() { const chartRef = ref<HTMLDivElement | null>()//可以绑定为div或者任意组件 const radioRef = ref<RadioInstance>() //elradio的ref const customComponentoRef = ref<InstanceType<typeof CustomComponent>>() //自定义组件的ref 写法,通过ref.value.xxx调用子组件方式时,子组件中一定要把xxx这个方法return出来 onMounted(() => { console.log(chartRef.value) // Element div... }) return { chartRef } }, render() { return <div ref={el => this.chartRef = el} style="{{width: 200px, height: 100px}}"></div> } })