# 内置组件
# Fragment
在 Vue3 中,组件可以没有根标签,内部会将多个根标签包含在一个 Fragment 虚拟元素中
# Teleport
传送门
有时组件模板的一部分逻辑上属于该组件,而从技术角度来看,最好将模板的这一部分移动到 DOM 中 Vue app 之外的其他位置。
比如模态框,这里通过to
来指定父元素的一个选择器
<template>
<h2>Teleport</h2>
<button @click="modalOpen = true">Open full screen modal! (With teleport!)</button>
<Teleport to="body">
<div v-if="modalOpen" class="modal">
<div>
<p>I'm a teleported modal! (My parent is "body")</p>
<button @click="modalOpen = false">Close</button>
</div>
</div>
</Teleport>
</template>
<script setup>
import { Teleport, ref } from "vue";
let modalOpen = ref(false);
</script>
<style>
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.modal div {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: white;
width: 400px;
height: 400px;
padding: 5px;
}
</style>
# Suspence
悬念
异步组件
<template>
<h2>TodoList</h2>
<p>{{ posts }}</p>
</template>
<script setup>
import sleep from "@/utils/sleep";
const res = await sleep(1000);
const posts = await [{ id: 1 }];
</script>
<template>
<h2>Suspence</h2>
<suspense>
<template #default>
<TodoList />
</template>
<template #fallback>
<div>Loading...</div>
</template>
</suspense>
</template>
<script setup>
import { defineAsyncComponent } from "vue";
let TodoList = defineAsyncComponent(() => import("./TodoList.vue"));
</script>
# Transiton
过渡
.v-enter-from,
.vue-leave-to {
opacity: 0;
}
.v-leave-from,
.vue-enter-to {
opacity: 1;
}