Vue3中的動(dòng)態(tài)組件詳解
Vue3動(dòng)態(tài)組件
動(dòng)態(tài)組件的基本使用
動(dòng)態(tài)組件(Dynamic Components)是一種在 Vue 中根據(jù)條件或用戶輸入來動(dòng)態(tài)渲染不同組件的技術(shù)。
在 Vue 中使用動(dòng)態(tài)組件,可以使用 <component> 元素,并通過 is 特性綁定一個(gè)組件的名稱或組件對象。通過在父組件中改變 is 特性的值,可以動(dòng)態(tài)切換渲染的組件。
第一種寫法
- A.vue
<template>
<div>
A component
</div>
</template>
<script setup lang="ts">
</script>
<style scoped></style>B.vue, C.vue 同理
- APP.vue
<template>
<div style="display: flex;">
<!-- class可以寫兩個(gè),一個(gè)靜態(tài),一個(gè)動(dòng)態(tài) -->
<div @click="switchCom(item, index)" :class="[active == index ? 'active' : '']" class="tabs"
v-for="(item, index) in data">
<div>{{ item.name }}</div>
</div>
</div>
<component :is="comId"></component>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue';
import AVue from './components/A.vue'
import BVue from './components/B.vue'
import CVue from './components/C.vue'
// 這里不需要將對象中所有數(shù)據(jù)變?yōu)轫憫?yīng)式,可以使用ref
const comId = ref(AVue)
const active = ref(0)
const switchCom = (item, index) => {
comId.value = item.com
active.value = index
}
const data = reactive([
{
name: 'A',
com: AVue
},
{
name: 'B',
com: BVue
},
{
name: 'C',
com: CVue
}
])
</script>
<style lang="scss" scoped>
.active {
background: blueviolet;
}
.tabs {
border: 1px solid #ccc;
padding: 5px 10px;
margin: 5px;
cursor: pointer;
}
</style>
第二種寫法
- APP.vue
<template>
<div style="display: flex;">
<!-- class可以寫兩個(gè),一個(gè)靜態(tài),一個(gè)動(dòng)態(tài) -->
<div @click="switchCom(item, index)" :class="[active == index ? 'active' : '']" class="tabs"
v-for="(item, index) in data">
<div>{{ item.name }}</div>
</div>
</div>
<component :is="comId"></component>
</template>
<script setup lang="ts">
// markRaw:作用:標(biāo)記一個(gè)對象,使其永遠(yuǎn)不會再成為響應(yīng)式對象。
import { ref, reactive, markRaw, shallowRef } from 'vue';
// 這里不需要將對象中所有數(shù)據(jù)變?yōu)轫憫?yīng)式,可以使用ref
const comId = shallowRef('AVue')
const active = ref(0)
const switchCom = (item, index) => {
comId.value = item.com
console.log(comId.value);
active.value = index
}
const data = reactive([
{
name: 'A',
com:'AVue'
},
{
name: 'B',
com:'BVue'
},
{
name: 'C',
com:'CVue'
}
])
</script>
<script lang="ts">
import AVue from './components/A.vue'
import BVue from './components/B.vue'
import CVue from './components/C.vue'
export default {
components: {
AVue,
BVue,
CVue
}
}
</script>
<style lang="scss" scoped>
.active {
background: blueviolet;
}
.tabs {
border: 1px solid #ccc;
padding: 5px 10px;
margin: 5px;
cursor: pointer;
}
</style>性能優(yōu)化
上述第一種寫法代碼會出現(xiàn)警告

輸出 comId 的值,出現(xiàn) comId 的屬性被劫持,出現(xiàn)性能浪費(fèi)

解決方法
使用markRaw和shallowRef這兩個(gè)API
- App.vue
<template>
<div style="display: flex;">
<!-- class可以寫兩個(gè),一個(gè)靜態(tài),一個(gè)動(dòng)態(tài) -->
<div @click="switchCom(item, index)" :class="[active == index ? 'active' : '']" class="tabs"
v-for="(item, index) in data">
<div>{{ item.name }}</div>
</div>
</div>
<component :is="comId"></component>
</template>
<script setup lang="ts">
// markRaw:作用:標(biāo)記一個(gè)對象,使其永遠(yuǎn)不會再成為響應(yīng)式對象。
import { ref, reactive, markRaw, shallowRef } from 'vue';
import AVue from './components/A.vue'
import BVue from './components/B.vue'
import CVue from './components/C.vue'
// 這里不需要將對象中所有數(shù)據(jù)變?yōu)轫憫?yīng)式,可以使用ref
const comId = shallowRef(AVue)
const active = ref(0)
const switchCom = (item, index) => {
comId.value = item.com
console.log(comId.value);
active.value = index
}
const data = reactive([
{
name: 'A',
com: markRaw(AVue)
},
{
name: 'B',
com: markRaw(BVue)
},
{
name: 'C',
com: markRaw(CVue)
}
])
</script>
<style lang="scss" scoped>
.active {
background: blueviolet;
}
.tabs {
border: 1px solid #ccc;
padding: 5px 10px;
margin: 5px;
cursor: pointer;
}
</style>再次輸出 comId 的值,解決性能浪費(fèi)的問題

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue Element使用icon圖標(biāo)教程詳解(第三方)
element-ui自帶的圖標(biāo)庫不夠全,還是需要需要引入第三方icon。下面小編給大家?guī)砹薞ue Element使用icon圖標(biāo)教程,感興趣的朋友一起看看吧2018-02-02
vue實(shí)現(xiàn)全選組件封裝實(shí)例詳解
這篇文章主要介紹了vue?全選組件封裝,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
vue-meta實(shí)現(xiàn)router動(dòng)態(tài)設(shè)置meta標(biāo)簽的方法
這篇文章主要介紹了vue-meta實(shí)現(xiàn)router動(dòng)態(tài)設(shè)置meta標(biāo)簽,實(shí)現(xiàn)思路非常簡單內(nèi)容包括mata標(biāo)簽的特點(diǎn)和mata標(biāo)簽共有兩個(gè)屬性,分別是http-equiv屬性和name屬性,本文通過實(shí)例代碼給大家詳細(xì)講解需要的朋友可以參考下2022-11-11
在vue.js中使用JSZip實(shí)現(xiàn)在前端解壓文件的方法
今天小編就為大家分享一篇在vue.js中使用JSZip實(shí)現(xiàn)在前端解壓文件的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue+Openlayer使用modify修改要素的完整代碼
這篇文章主要介紹了Vue+Openlayer使用modify修改要素的完整代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
vue Treeselect 樹形下拉框:獲取選中節(jié)點(diǎn)的ids和lables操作
這篇文章主要介紹了vue Treeselect 樹形下拉框:獲取選中節(jié)點(diǎn)的ids和lables操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
node+vue前后端分離實(shí)現(xiàn)登錄時(shí)使用圖片驗(yàn)證碼功能
這篇文章主要介紹了node+vue前后端分離實(shí)現(xiàn)登錄時(shí)使用圖片驗(yàn)證碼,記錄前端使用驗(yàn)證碼登錄的過程,后端用的是node.js,關(guān)鍵模塊是svg-captcha,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11

