Vue3提供了一种方便的方式去获取组件实例,本文将从以下多个方面对Vue3获取组件实例做详细的阐述。
一、使用$ref获取实例
Vue3中使用$ref获取组件实例跟Vue2的用法基本一致,只不过需要使用Vue3提供的ref函数来进行实例的绑定。下面我们将通过示例来演示如何使用$ref获取组件实例。
const app = Vue.createApp({
setup() {
const myComponentRef = Vue.ref(null);
return {
myComponentRef
};
}
});app.component('my-component', {
template: `
{{ message }}原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/300266.html