最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

uniapp?動態(tài)組件實現(xiàn)Tabs標(biāo)簽切換組件(喜馬拉雅app作為案例)

 更新時間:2023年10月11日 14:57:23   作者:tengyuxin  
本文以喜馬拉雅app作為案例給大家詳解講解uniapp?動態(tài)組件實現(xiàn)Tabs標(biāo)簽切換組件功能,在uniapp中實現(xiàn)動態(tài)組件切換需看uniapp是否支持,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

uniapp 動態(tài)組件實現(xiàn)Tabs標(biāo)簽切換組件

 一、功能描述 

喜馬拉雅app作為案例,它的tabs標(biāo)簽有很多,如:推薦、小說、兒童等等。用戶點擊小說就會切換到小說組件,如果仔細(xì)看第一次是有一個加載的過程,如果你再返回到推薦頁,再次點擊小說時就沒有加載過程,而是直接顯示出來。

這就有兩個知識點
(1)動態(tài)組件切換 (vue3 中 component組件 is屬性)

(2)保持組件被移除時的狀態(tài)(vue3 中是 KeepAlive)

二、在uniapp中實現(xiàn)動態(tài)組件切換需看uniapp是否支持

從官方文檔看到,component是支持 App 和 H5的(我實在app上實現(xiàn)的),而keepAlive只支持H5

<template>
    <!-- 標(biāo)簽數(shù)據(jù) -->
    <u-tabs class="tabsBox" 
			:list="tabData" @click="changeTab" :scrollable="false" 
			lineColor="#000"
			:activeStyle="{color: '#000',fontWeight: 'bold',transform: 'scale(1.05)'}" 
			:inactiveStyle="{color: '#000'}"
	></u-tabs>
    <!-- 組件切換 -->
	<component :is="tabData[current].comName"></component>
</template>
<script setup>
	import {ref,reactive,onMounted,markRaw} from 'vue';
    import A from "./a.vue";
    import B from "./b.vue";
    import C from "./c.vue";
    let tabData = reactive([
		{name: '體育',comName:markRaw(A)},
		{name: '經(jīng)濟(jì)',comName:markRaw(B)},
		{name: 'IT信息',comName:markRaw(C)}
	]);
    //切換標(biāo)簽事件
	let current = ref(0);
	const changeTab = (obj)=>{
		current.value = obj.index;
	}
</script>

解釋一下:

(1)u-tabs 是 uview-plus 里面的標(biāo)簽組件

(2)markRaw 將一個對象標(biāo)記為不可被轉(zhuǎn)為代理。返回該對象本身,不然用響應(yīng)式的ref 會報出警告

關(guān)于如何保持組件被移除時的狀態(tài),我也在尋找方案

uniapp實現(xiàn)tab分類切換

<template>
    <view class="content">
        <view class="nav">
            <!-- 選項卡水平方向滑動,scroll-with-animation是滑動到下一個選項時,有一個延時效果 -->
            <scroll-view class="tab-scroll" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
                <view class="tab-scroll_box">
                    <!-- 選項卡類別列表 -->
                    <view class="tab-scroll_item" v-for=" (item,index) in category" :key="index"  :class="{'active':isActive==index}" @click="chenked(index)">
                            {{item.name}}
                    </view>
                </view>
            </scroll-view>
        </view>
        <!-- 選項卡內(nèi)容輪播滑動顯示,current為當(dāng)前第幾個swiper子項 -->
        <swiper @change="change" :current="isActive" class="swiper-content" :style="fullHeight">
            <swiper-item class="swiperitem-content">
                <scroll-view scroll-y style="height: 100%;">
                    <view class="nav_item" v-for="(item,index) in add":key="index" >
                        <view class="">
                            {{item.name}}
                        </view>
                        <view class="">
                            {{item.num}}
                        </view>
                        <view class="">
                            {{item.pink}}
                        </view>
                    </view>
                </scroll-view>    
            </swiper-item>
        </swiper>
    </view>
</template>
<script>
    export default {
        watch:{
            // swiper與上面選項卡聯(lián)動
            currentindex(newval){
                this.isActive = newval;
                this.scrollLeft = 0;
                // 滑動swiper后,每個選項距離其父元素最左側(cè)的距離
                for (let i = 0; i < newval - 1; i++) {
                    this.scrollLeft += this.category[i].width
                };
            },
        },
        data() {    
            return {
                isActive: 0,
                index: 0,
                currentindex:0,
                category:[
                    {
                        id:1,
                        name:'選項卡一',
                    },
                    {
                        id:2,
                        name:'選項卡二',
                    },
                    {
                        id:3,
                        name:'選項卡三',
                    },
                    {
                        id:4,
                        name:'選項卡四',
                    },
                    {
                        id:5,
                        name:'選項卡五',
                    },
                    {
                        id:6,
                        name:'選項卡六',
                    },
                ],
                contentScrollW: 0, // 導(dǎo)航區(qū)寬度
                scrollLeft: 0, // 橫向滾動條位置
                fullHeight:"",    
                arr:[
                    {
                        name:'123444',
                        num:'dfrgthh',
                        pink:'1000',
                        type:1
                    },{
                        name:'kkkkkkkk',
                        num:'dfrgthh',
                        pink:'1000',
                        type:1
                    },{
                        name:'123444',
                        num:'dfrgthh',
                        pink:'1000',
                        type:1
                    },{
                        name:'vvvvvvvv',
                        num:'dfrgthh',
                        pink:'1000',
                        type:2
                    },{
                        name:'aaaaaaaaa',
                        num:'dfrgthh',
                        pink:'1000',
                        type:3
                    },{
                        name:'gggggggg',
                        num:'dfrgthh',
                        pink:'1000',
                        type:5
                    },{
                        name:'ddddddd',
                        num:'dfrgthh',
                        pink:'1000',
                        type:5
                    },
                ],
            add:[]
            }
        },
        mounted() {
            var that = this;
             //獲取手機(jī)屏幕的高度,讓其等于swiper的高度,從而使屏幕充滿
            uni.getSystemInfo({
                  success: function (res) {
                         that.fullHeight ="height:" + res.windowHeight + "px";
                  }
                });
            // 獲取標(biāo)題區(qū)域?qū)挾?,和每個子元素節(jié)點的寬度
            this.getScrollW()
        },
        methods: {
            // 獲取標(biāo)題區(qū)域?qū)挾?,和每個子元素節(jié)點的寬度以及元素距離左邊欄的距離
            getScrollW() {
                const query = uni.createSelectorQuery().in(this);
                query.select('.tab-scroll').boundingClientRect(data => {
                      // 拿到 scroll-view 組件寬度
                      this.contentScrollW = data.width
                 }).exec();
                 query.selectAll('.tab-scroll_item').boundingClientRect(data => {
                     let dataLen = data.length;
                      for (let i = 0; i < dataLen; i++) {
                          //  scroll-view 子元素組件距離左邊欄的距離
                          this.category[i].left = data[i].left;
                         //  scroll-view 子元素組件寬度
                         this.category[i].width = data[i].width
                    }
                 }).exec()
            },
            // 當(dāng)前點擊子元素靠左留一個選項展示,子元素寬度不相同也可實現(xiàn)
            chenked(index) {
                if(index == 0){
                    this.add = this.arr
                }else{
                    let ass = []
                    for(let i in this.arr){
                        if(this.arr[i].type == index){
                            ass.push(this.arr[i])
                        }
                    }
                    this.add =ass
                }
                console.log(this.add);
                // this.isActive = index;
                // this.scrollLeft = 0;
                // for (let i = 0; i < index - 1; i++) {
                //     this.scrollLeft += this.category[i].width
                // };
            },
            // swiper滑動時,獲取其索引,也就是第幾個
            change(e){
                const {current} = e.detail;
                this.currentindex = current;
            },    
        }
    }
</script>
<style lang="scss">
    page{
        height: 100%;
        display: flex;
        background-color: #FFFFFF;
    }
    .content{
        display: flex;
        flex-direction: column;
        width: 100%;
        flex: 1;
        .nav{
            border-top: 1rpx solid #f2f2f2;
            background-color: #fceeee;    
            position: fixed;
            z-index: 99;
            width: 100%;
            align-items: center;
            height: 100rpx;
            .tab-scroll{
                flex: 1;
                overflow: hidden;
                box-sizing: border-box;
                padding-left: 30rpx;
                padding-right: 30rpx;
                .tab-scroll_box{
                    display: flex;
                    align-items: center;
                    flex-wrap: nowrap;
                    box-sizing: border-box;
                    .tab-scroll_item{
                        line-height: 60rpx;
                        margin-right: 35rpx;
                        flex-shrink: 0;
                        padding-bottom:10px;
                        display: flex;
                        justify-content: center;
                        font-size: 16px;
                        padding-top: 10px;
                    }
                }
            }
        }
        .swiper-content{
               padding-top: 120rpx;
               flex: 1;
            .swiperitem-content{
                background-color: #ffffff;
                .nav_item{    
                    background-color: #FFFFFF;
                    padding:20rpx 40rpx 0rpx 40rpx ;
                }
            }
        }    
    }
    .active {
        position: relative;
        color: #ff0000;
        font-weight: 600;
    }
    .active::after {
        content: "";
        position: absolute;
        width: 130rpx;
        height: 4rpx;
        background-color: #ff0000;
        left: 0px;
        right: 0px;
        bottom: 0px;
        margin: auto;
    }
    /* 隱藏滾動條,但依舊具備可以滾動的功能 */
    /deep/.uni-scroll-view::-webkit-scrollbar {
        display: none
    }
</style>

到此這篇關(guān)于uniapp 動態(tài)組件實現(xiàn)Tabs標(biāo)簽切換組件的文章就介紹到這了,更多相關(guān)uniapp Tabs標(biāo)簽切換組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

安吉县| 正阳县| 孟州市| 阜南县| 汶川县| 肥城市| 同德县| 武胜县| 宁强县| 江永县| 富川| 东阳市| 灵宝市| 河南省| 云浮市| 日喀则市| 永宁县| 永仁县| 自治县| 资源县| 芦溪县| 临江市| 商河县| 乐陵市| 黎城县| 鄂伦春自治旗| 望都县| 鸡泽县| 时尚| 久治县| 东海县| 兴义市| 永善县| 北碚区| 健康| 嵊州市| 苏州市| 合水县| 醴陵市| 长沙县| 高安市|