四、Composition API 的優(yōu)勢(shì)
1. Options API 存在的問題
使用傳統(tǒng)OptionsAPI中,新增或者修改一個(gè)需求,就需要分別在data,methods,computed里修改。
2. Composition API 的優(yōu)勢(shì)
我們可以更加優(yōu)雅的組織我們的代碼,函數(shù)。讓相關(guān)功能的代碼更加有序的組織在一起。
五、新的組件
1. Fragment
- 在Vue2中:組件必須有一個(gè)根標(biāo)簽
- 在Vue3中:組件可以沒有根標(biāo)簽,內(nèi)部會(huì)將多個(gè)標(biāo)簽包含在一個(gè)Fragment虛擬元素中
- 好處:減少標(biāo)簽層級(jí),減小內(nèi)存占用
2. Teleport
- 什么是Teleport?
——Teleport (傳送)是一種能夠?qū)⑽覀兊慕M件html結(jié)構(gòu)移動(dòng)到指定位置的技術(shù)。
Dialog.vue
<template>
<div>
<button @click="isShow = true">點(diǎn)我彈窗</button>
<teleport to="body">
<div v-if="isShow" class="mask">
<div class="dialog">
<h3>我是一個(gè)彈窗</h3>
<h4>一些內(nèi)容</h4>
<h4>一些內(nèi)容</h4>
<h4>一些內(nèi)容</h4>
<button @click="isShow = false">關(guān)閉彈窗</button>
</div>
</div>
</teleport>
</div>
</template>
<script >
import {ref} from 'vue'
export default {
name: 'Dialog',
setup(){
let isShow = ref(false)
return {isShow}
}
}
</script>
<style>
.mask {
position: absolute;
top: 0;bottom: 0; left: 0; right: 0;
background-color: rgba(0, 0, 0, 0.5);
}
.dialog {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
text-align: center;
width: 300px;
height: 300px;
background-color: green;
}
</style>
Son.vue
<template>
<div class="Son">
<h3>我是Son組件</h3>
<Dialog></Dialog>
</div>
</template>
<script >
import Dialog from './Dialog.vue'
export default {
name: 'Son',
components: {Dialog}
}
</script>
<style>
.Son {
background-color: orange;
padding: 10px;
}
</style>
Child.vue文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-434705.html
<template>
<div class="Child">
<h3>我是Child組件</h3>
<Son></Son>
</div>
</template>
<script >
import Son from './Son.vue'
export default {
name: 'Child',
components: {Son},
}
</script>
<style>
.Child {
background-color: skyblue;
padding: 10px;
}
</style>
3. Suspense
- 等待異步組件時(shí)渲染一些額外內(nèi)容,讓應(yīng)用有更好的用戶體驗(yàn)
- 使用步驟:
- 引入異步組件
import { defineAsyncComponent } from 'vue';//靜態(tài)引入
const Child = defineAsyncComponent(() => import('./components/Child.vue'))//異步引入
- 使用 Suspense 包裹組件,并配置好default 與 fallback
<template>
<div class="app">
<h3>我是App組件</h3>
<Suspense>
<template v-slot:default>
<Child></Child>
</template>
<template v-slot:fallback>
<h3>稍等,加載中...</h3>
</template>
</Suspense>
</div>
</template>
六、其他
1. 全局API的轉(zhuǎn)移
- Vue2.x有許多全局API 和 配置。
- 例如:注冊(cè)全局組件、注冊(cè)全局指令等。
//注冊(cè)全局組件
Vue.component('MyButton', {
data: () => ({
count: 0
}),
template: '<button @click="count++">Clicked {{ count }} times.</button>'
})
//注冊(cè)全局指令
Vue.directive('focus', {
inserted: el => el.focus()
}
- Vue3.0 中對(duì)這些API做出了調(diào)整:
- 將全局的API,即:Vue.xxx 調(diào)整到應(yīng)用實(shí)例(app)上
2.x全局API(vue) | 3.x 實(shí)例API(app) |
---|---|
Vue.config.xxxx | app.config.xxxx |
Vue.config.production Tip | 移除 |
Vue.component | app.component |
Vue.directive | app.directive |
Vue.mixin | app.mixin |
Vue.use | app.use |
Vue.prototype | app.config.globalProperties |
2.其他改變
- data選項(xiàng)應(yīng)始終被聲明為一個(gè)函數(shù)。
- 過(guò)度類名的更改:
- Vue2.x寫法
.v-enter,
.v-leave-to {
opacity: 0;
}
.v-leave,
.v-enter-to {
opacity: 1;
}
- Vue3.x寫法
.v-enter-from,
.v-leave-to {
opacity: 0;
}
.v-leave-from,
.v-enter-to {
opacity: 1;
}
-
移除keyCode作為 v-on 的修飾符,同時(shí)也不再支持
configkeyCodes
-
移除
v-on.native
修飾符- 父組件中綁定事件
<my-component
v-on:close="handleComponentEvent"
v-on:click="handleNativeClickEvent"
/>
- 子組件中聲明自定義事件
<script>
export default {
emits: ['close']
}
</script>
- 移除過(guò)濾器(filter)
過(guò)濾器雖然這看起來(lái)很方便,但它需要一個(gè)自定義語(yǔ)法,打破大括號(hào)內(nèi)表達(dá)式是“只是JavaScript”的假設(shè),這不僅有學(xué)習(xí)成本,而且有實(shí)現(xiàn)成本!建議用方法調(diào)用或計(jì)算屬性去替換過(guò)濾器。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-434705.html
到了這里,關(guān)于Composition API 的優(yōu)勢(shì)、新的組件(Fragment,Teleport,Suspense)【Vue3】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!