vue3?ref獲取組件實例詳細(xì)圖文教程
1.ref獲取組件實例時前面不要寫冒號
需要注意的是通過ref拿到組件的屬性或方法必須是子組件return出來的
具體如下
<!--tempaleteb標(biāo)簽的內(nèi)容--> <!-- 注意:ref前面不能有冒號 --> ? ? <h1 ref="title">我是標(biāo)題</h1> ? ? <child ref="child"></child>? ? ? ? ? ? ? //setup函數(shù)內(nèi)的內(nèi)容 ?// 通過ref獲取組件實例 ? ? const child = ref(null) ? ? const title = ref(null) //掛載完成后獲取實例 ? ? onMounted(() => { ? ? ? ? console.log(child.value) ? ? ? ? console.log(title.value) ? ? ? ? child.value.hh() ? ? })
效果圖如下
2.組件介紹
Fragment 組件
在 vue2.x 中組件模板必須要一個根標(biāo)簽;但是在 vue3.x 中不再需要一個根標(biāo)簽,它會自 動創(chuàng)建一個 Fragment
<template> <div>我是描述</div> <h3>我是標(biāo)題</h3> </template> <script> export default {}; </script> <style></style>
3.Suspense 組件
加載異步組件的時候,渲染一些其他內(nèi)容
App.vue
<template> ? <div class="app"> ? ? <Suspense> ? ? ? <template v-slot:default> ? ? ? ? <Child /> ? ? ? </template> ? ? ? <template v-slot:fallback> ? ? ? ? <h1>加載中...</h1> ? ? ? </template> ? ? </Suspense> ? </div> </template> <script> // import Child from './Child.vue'; // 程序開始就會打包編譯 // 導(dǎo)入defineAsyncComponent 方法 定義異步加載組件 import { defineAsyncComponent } from "vue"; const Child = defineAsyncComponent(() => import("./Child.vue")); export default { ? components: { ? ? Child, ? }, }; </script> <style scoped> .app { ? background-color: #eee; ? padding: 30px; } </style>
child.vue
<template> <div class="child">我是子組件</div> </template> <script> export default {}; </script> <style scoped> .child { border: 2px solid red; margin: 20px; padding: 20px; } </style>
4.Teleport 組件
作用: 將指定 DOM 內(nèi)容移動到指定的某個節(jié)點里面(可以理解為將組件掛載到指定節(jié)點上面) 使用場景: 彈框、播放器組件的定位
dialog.vue
<template> <div class="dialog">我是彈框</div> </template> <script> export default {}; </script> <style scoped> .dialog { width: 300px; height: 300px; padding: 30px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); } </style>
app.vue
<template> <div class="app"> <h3>我是標(biāo)題</h3> <h1>我是一級標(biāo)題</h1> <div id="test"> <!-- to屬性的值為選擇器,表示放在哪個節(jié)點下面 --> <teleport to="body"> <Dialog /> </teleport> </div> </div> </template> <script> import Dialog from "./Dialog.vue"; export default { components: { Dialog, }, }; </script> <style scoped> .app { background-color: #eee; padding: 30px; } </style>
運行結(jié)果
總結(jié)
到此這篇關(guān)于vue3 ref獲取組件實例的文章就介紹到這了,更多相關(guān)vue3 ref獲取組件實例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue與vue-i18n結(jié)合實現(xiàn)后臺數(shù)據(jù)的多語言切換方法
下面小編就為大家分享一篇vue與vue-i18n結(jié)合實現(xiàn)后臺數(shù)據(jù)的多語言切換方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03使用Element-UI的el-tabs組件,瀏覽器卡住了的問題及解決
這篇文章主要介紹了使用Element-UI的el-tabs組件,瀏覽器卡住了的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08vue使用動態(tài)組件實現(xiàn)TAB切換效果完整實例
在實際項目開發(fā)中,我們經(jīng)常會遇到選項卡切換,對于一個前端工程師來說,組件化/模塊化開發(fā)是一種必備的行為規(guī)范,下面這篇文章主要給大家介紹了關(guān)于vue使用動態(tài)組件實現(xiàn)TAB切換效果的相關(guān)資料,需要的朋友可以參考下2023-05-05Vue系列之Element?UI表單自定義校驗規(guī)則
表單校驗是注冊環(huán)節(jié)中必不可少的操作,表單校驗可以提醒用戶填寫數(shù)據(jù)規(guī)則以確保用戶提交數(shù)據(jù)的效性,也可以防止用戶因誤操作而占用服務(wù)器資源,這篇文章主要給大家介紹了關(guān)于Vue系列之Element?UI表單自定義校驗規(guī)則的相關(guān)資料,需要的朋友可以參考下2022-09-09vue實現(xiàn)多個el-form表單提交統(tǒng)一校驗的2個方法
這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)多個el-form表單提交統(tǒng)一校驗的2個方法,文中通過代碼示例介紹的非常詳細(xì),對大家學(xué)習(xí)或使用vue具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07Vue-drag-resize 拖拽縮放插件的使用(簡單示例)
本文通過代碼給大家介紹了Vue-drag-resize 拖拽縮放插件使用簡單示例,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12使用vue-cli3 創(chuàng)建vue項目并配置VS Code 自動代碼格式化 vue語法高亮問題
這篇文章主要介紹了使用vue-cli3 創(chuàng)建vue項目,并配置VS Code 自動代碼格式化 vue語法高亮問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05