vue、uniapp實(shí)現(xiàn)組件動(dòng)態(tài)切換效果
在Vue中,通過使用動(dòng)態(tài)組件,我們可以實(shí)現(xiàn)組件的動(dòng)態(tài)切換,從而達(dá)到頁面的動(dòng)態(tài)展示效果。
vue 中 component組件 is屬性
功能描述
例如:有多個(gè)tabs標(biāo)簽,如:推薦、熱點(diǎn)、視頻等。用戶點(diǎn)擊標(biāo)簽就會(huì)切換到對(duì)應(yīng)組件
vue2版
<template>
<!-- 標(biāo)簽數(shù)據(jù) -->
<!-- uview-ui 標(biāo)簽組件 -->
<u-tabs
class="tabsBox"
:list="tabData"
@click="changeTab"
:current="tabsCurrent"
></u-tabs>
<!-- 組件切換 -->
<component :is="getCurrentCompName"></component>
</template>
<script>
import CompA from './components/comp-a.vue'
import CompB from './components/comp-b.vue'
import CompC from './components/comp-c.vue'
export default {
data() {
return {
tabsCurrent: 0,
tabsList: [],
}
},
computed: {
getCurrentCompName() {
let currentCompName = ''
switch (this.tabsCurrent) {
case 1:
currentCompName = 'CompB'
break
case 2:
currentCompName = 'CompC'
break
default:
currentCompName = 'CompA'
}
return currentCompName
},
},
methods: {
toggle(index) {
this.tabsCurrent = index
},
},
}
</script>vue3版
<template>
<!-- 標(biāo)簽數(shù)據(jù) -->
<!-- uview-ui 標(biāo)簽組件 -->
<u-tabs
class="tabsBox"
:list="tabData"
@click="changeTab"
:current="tabsCurrent"
></u-tabs>
<!-- 組件切換 -->
<component :is="getCurrentCompName"></component>
</template>
<script setup>
import { ref, reactive, markRaw} from 'vue';
import CompA from './components/comp-a.vue';
import CompB from './components/comp-b.vue';
import CompC from './components/comp-c.vue';
const tabsCurrent = ref(0);
const tabsList = ref([]);
const getCurrentCompName = () => {
let currentCompName = '';
switch (tabsCurrent.value) {
case 1:
currentCompName = markRaw(CompB);
break;
case 2:
currentCompName = markRaw(CompC);
break;
default:
currentCompName = markRaw(CompA);
}
return currentCompName;
};
const toggle = (index) => {
tabsCurrent.value = index;
};
</script>或者
<template>
<!-- 標(biāo)簽數(shù)據(jù) -->
<!-- uview-ui 標(biāo)簽組件 -->
<u-tabs
class="tabsBox"
:list="tabData"
@click="changeTab"
:current="tabsCurrent"
></u-tabs>
<!-- 組件切換 -->
<component :is="currentComp"></component>
</template>
<script setup>
import { ref, reactive, markRaw, shallowRef } from 'vue';
import CompA from './components/comp-a.vue';
import CompB from './components/comp-b.vue';
import CompC from './components/comp-c.vue';
const tabsCurrent = ref(0);
const tabsList = ref([]);
const currentComp = shallowRef(CompA)
const toggle = (index) => {
tabsCurrent.value = index;
switch (index) {
case 1:
currentComp.value = CompB;
break;
case 2:
currentComp.value = CompC;
break;
default:
currentComp.value = CompA;
}
};
</script>到此這篇關(guān)于vue、uniapp實(shí)現(xiàn)組件動(dòng)態(tài)切換的文章就介紹到這了,更多相關(guān)vue uniapp組件動(dòng)態(tài)切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在uniapp中實(shí)現(xiàn)中英文切換的方法
- uniapp?動(dòng)態(tài)組件實(shí)現(xiàn)Tabs標(biāo)簽切換組件(喜馬拉雅app作為案例)
- uniapp單頁面實(shí)現(xiàn)頁面切換的使用示例
- uniapp實(shí)現(xiàn)tabs切換(可滑動(dòng))效果實(shí)例
- uniapp實(shí)現(xiàn)全局設(shè)置字體大小(小中大的字體切換)
- uniapp組件之tab選項(xiàng)卡滑動(dòng)切換功能實(shí)現(xiàn)
- Uniapp 實(shí)現(xiàn)頂部標(biāo)簽頁切換功能(詳細(xì)步驟)
相關(guān)文章
vue如何給element-ui中的el-tabs動(dòng)態(tài)設(shè)置label屬性
這篇文章主要介紹了vue如何給element-ui中的el-tabs動(dòng)態(tài)設(shè)置label屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue的export?default和帶返回值的data()及@符號(hào)的用法說明
這篇文章主要介紹了Vue的export?default和帶返回值的data()及@符號(hào)的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue實(shí)現(xiàn)登錄數(shù)據(jù)的持久化的使用示例
在Vue.js中,實(shí)現(xiàn)登錄數(shù)據(jù)的持久化需要使用瀏覽器提供的本地存儲(chǔ)功能,Vue.js支持使用localStorage和sessionStorage來實(shí)現(xiàn)本地存儲(chǔ),本文就來介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2023-10-10
vue.js高德地圖實(shí)現(xiàn)熱點(diǎn)圖代碼實(shí)例
這篇文章主要介紹了vue.js高德地圖實(shí)現(xiàn)熱點(diǎn)圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue級(jí)聯(lián)選擇器的getCheckedNodes使用方式
這篇文章主要介紹了vue級(jí)聯(lián)選擇器的getCheckedNodes使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Element輸入框帶歷史查詢記錄的實(shí)現(xiàn)示例
這篇文章主要介紹了Element輸入框帶歷史查詢記錄的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Vue中引入echarts的步驟及折線圖、柱狀圖常見配置項(xiàng)
這篇文章主要介紹了Vue中引入echarts的步驟及折線圖、柱狀圖常見配置項(xiàng),需要的朋友可以參考下2023-11-11

