vue3的內(nèi)置組件匯總
官方給出的說明:
Fragment: Vue 3 組件不再要求有一個(gè)唯一的根節(jié)點(diǎn),清除了很多無用的占位 div。Teleport: 允許組件渲染在別的元素內(nèi),主要開發(fā)彈窗組件的時(shí)候特別有用。Suspense: 異步組件,更方便開發(fā)有異步請(qǐng)求的組件。
一、fragment 片斷組件(了解)
- 在 Vue2 中:組件必須有一個(gè)根標(biāo)簽。
- 在 Vue3 中:組件可以沒有根標(biāo)簽,內(nèi)部會(huì)將多個(gè)標(biāo)簽包含在一個(gè) fragment 的虛擬元素中。
這樣的好處是:減少標(biāo)簽層級(jí),減小內(nèi)存占用。
二、teleport 瞬移組件
Teleport 是一種能夠?qū)?“我們的組件 html 結(jié)構(gòu)” 移動(dòng)到指定位置的技術(shù)。
使用 teleport 組件時(shí),需要指定 “移動(dòng)位置”:
<teleport to="移動(dòng)位置"></teleport>
例如:用 suspense 實(shí)現(xiàn)全屏彈窗
父組件A:
<template>
<div class="tel_a">
<h3>父組件A</h3>
<son />
</div>
</template>
<script setup>
import { defineAsyncComponent } from 'vue'
const Son = defineAsyncComponent(() => import('./components/son.vue'))
</script>
<style lang="less" scoped>
.tel_a {
width: 30%;
background: #aaa;
padding: 10px 30px;
}
</style>
子組件B:
<template>
<div class="tel_b">
<div>子組件B</div>
<button @click="modalOpen = true"> 打開全屏彈窗(teleport) </button>
<teleport to="body">
<div v-if="modalOpen" class="modal">
<div class="content">
我是一個(gè)teleport彈窗<br />(我的父組件是“body”)
<button @click="modalOpen = false"> 關(guān)閉 </button>
</div>
</div>
</teleport>
</div>
</template>
<script setup>
import { ref } from 'vue'
const modalOpen = ref(false)
</script>
<style lang="less" scoped>
.flex-center () {
display: flex;
align-items: center;
justify-content: center;
}
.tel_b {
padding: 10px;
background: rgba(242, 177, 57);
}
.modal {
.flex-center;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
.content {
.flex-center;
flex-direction: column;
text-align: center;
width: 260px;
height: 160px;
padding: 10px;
background-color: #fff;
button {
display: block;
margin-top: 30px;
}
}
}
</style>
效果如下:

三、suspense 組件
suspense 組件支持:在等待異步組件加載時(shí),渲染一些額外內(nèi)容。不必非得等異步組件加載完畢再渲染,避免了因異步加載帶來的白屏和閃屏問題的出現(xiàn),提高了用戶的體驗(yàn)。
suspense 的使用步驟:
- 先異步引入組件——
defineAsyncComponent。 - 然后用 suspense 包裹組件,并配置好
default和fallback。
先異步引入組件:
import { defineAsyncComponent } from 'vue'
const Child = defineAsyncComponent(()=>import('./components/Child.vue'))
然后用 Suspense 包裹組件:
<template>
<div class="app">
<h3>我是App組件</h3>
<Suspense>
<template #default>
<Child/>
</template>
<template #fallback>
<h3>加載中.....</h3>
</template>
</Suspense>
</div>
</template>到此這篇關(guān)于vue3的內(nèi)置組件匯總的文章就介紹到這了,更多相關(guān)vue3 內(nèi)置組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2.x+webpack快速搭建前端項(xiàng)目框架詳解
本文給大家介紹了vue2.x、webpack、vuex、sass+axios、elementUI等快速搭建前端項(xiàng)目框架的詳細(xì)操作方法,需要的跟著學(xué)習(xí)下吧。2017-11-11
關(guān)于vuejs中v-if和v-show的區(qū)別及v-show不起作用問題
v-if 有更高的切換開銷,而 v-show 有更高的出事渲染開銷.因此,如果需要非常頻繁的切換,那么使用v-show好一點(diǎn);如果在運(yùn)行時(shí)條件不太可能改變,則使用v-if 好點(diǎn)2018-03-03
解決ant Design中this.props.form.validateFields未執(zhí)行的問題
這篇文章主要介紹了解決ant Design中this.props.form.validateFields未執(zhí)行的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue子元素綁定的事件, 阻止觸發(fā)父級(jí)上的事件處理方式
這篇文章主要介紹了vue子元素綁定的事件, 阻止觸發(fā)父級(jí)上的事件處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
vue使用Element組件時(shí)v-for循環(huán)里的表單項(xiàng)驗(yàn)證方法
這篇文章主要介紹了vue使用Element組件時(shí)v-for循環(huán)里的表單項(xiàng)驗(yàn)證方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
vue自定義指令實(shí)現(xiàn)僅支持輸入數(shù)字和浮點(diǎn)型的示例
今天小編就為大家分享一篇vue自定義指令實(shí)現(xiàn)僅支持輸入數(shù)字和浮點(diǎn)型的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
vue3中storeToRefs讓store中的結(jié)構(gòu)出來的數(shù)據(jù)也能變成響應(yīng)式(推薦)
這篇文章主要介紹了vue3中storeToRefs讓store中的結(jié)構(gòu)出來的數(shù)據(jù)也能變成響應(yīng)式,本文通過實(shí)例代碼給大家介紹的分需要的朋友可以參考下2024-09-09

