最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3?ref獲取組件實例詳細圖文教程

 更新時間:2023年10月09日 09:37:15   作者:鯨落_Libra  
在Vue3中可以使用ref函數(shù)來創(chuàng)建一個響應(yīng)式的變量,通過將ref函數(shù)應(yīng)用于一個組件實例,我們可以獲取到該組件的實例對象,這篇文章主要給大家介紹了關(guān)于vue3?ref獲取組件實例的詳細圖文教程,需要的朋友可以參考下

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)文章

最新評論

滕州市| 灵台县| 柞水县| 彩票| 曲周县| 瑞丽市| 桂阳县| 蛟河市| 宝应县| 安龙县| 根河市| 延川县| 三门峡市| 射洪县| 瑞丽市| 乐清市| 盐边县| 北安市| 友谊县| 平江县| 泾阳县| 临江市| 黄浦区| 平乡县| 天长市| 池州市| 云龙县| 东光县| 文登市| 贡嘎县| 手机| 乌拉特中旗| 禄丰县| 中西区| 青冈县| 奉贤区| 神木县| 甘南县| 离岛区| 河北区| 通榆县|