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/zh-tw/n/300266.html