vue、uniapp實(shí)現(xiàn)組件動(dòng)態(tài)切換效果
在Vue中,通過(guò)使用動(dòng)態(tài)組件,我們可以實(shí)現(xiàn)組件的動(dòng)態(tài)切換,從而達(dá)到頁(yè)面的動(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單頁(yè)面實(shí)現(xiàn)頁(yè)面切換的使用示例
- 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)簽頁(yè)切換功能(詳細(xì)步驟)
相關(guān)文章
vue雙向綁定數(shù)據(jù)限制長(zhǎng)度的方法
這篇文章主要為大家詳細(xì)介紹了vue雙向綁定數(shù)據(jù)限制長(zhǎng)度的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
vue-cli如何關(guān)閉Uncaught?error的全屏提示
這篇文章主要介紹了vue-cli如何關(guān)閉Uncaught?error的全屏提示問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Element-Ui組件 NavMenu 導(dǎo)航菜單的具體使用
這篇文章主要介紹了Element-Ui組件 NavMenu 導(dǎo)航菜單的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
vite+vue3+ts項(xiàng)目新建以及解決遇到的問(wèn)題
vite是一個(gè)基于Vue3單文件組件的非打包開(kāi)發(fā)服務(wù)器,它具有快速的冷啟動(dòng),不需要等待打包操作,下面這篇文章主要給大家介紹了關(guān)于vite+vue3+ts項(xiàng)目新建以及解決遇到的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2023-06-06
el-form錯(cuò)誤提示信息手動(dòng)添加和取消的示例代碼
這篇文章主要介紹了el-form錯(cuò)誤提示信息手動(dòng)添加和取消,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08

