Skip to content

ref

获取dom元素

可以使用ref获取dom元素

js
<h1 ref="getDomRef">ref获取dom元素</h1>
<button @click="onGetH1Dom">获取dom元素</button>
const getDomRef = ref();
const onGetH1Dom = () => {
  console.log(getDomRef.value);
};

获取组件实例

可以使用ref获取组件实例

js
<my-component ref="getComponentRef"></my-component>
<button @   click="onGetComponent">获取组件实例</button>
const getComponentRef = ref();
const onGetComponent = () => {
  console.log(getComponentRef.value);
};

获取组件实例的属性或方法

可以使用ref获取组件实例的属性或方法

js
<my-component ref="getComponentInstanceRef"></my-component> 
<button @click="onGetComponentInstance">获取组件实例的属性或方法</button>
const getComponentInstanceRef = ref();
const onGetComponentInstance = () => {
  console.log(getComponentInstanceRef.value.someProperty);
  getComponentInstanceRef.value.someMethod();
};

Released Under The MIT License.