Vue:解決 element-ui 下拉框過(guò)多導(dǎo)致卡頓問(wèn)題

標(biāo)簽: Vue element-ui


原因:下拉框數(shù)據(jù)過(guò)多,若渲染全部數(shù)據(jù),會(huì)導(dǎo)致 DOM 數(shù)量太多,操作卡頓。

解決辦法:將獲取的數(shù)據(jù)(allList)和渲染數(shù)據(jù)(list)分離開(kāi),限制渲染數(shù)組的長(zhǎng)度。

<template>
    <el-select v-model="value" filterable :filter-method="filterList" placeholder="請(qǐng)選擇">
        <el-option
            v-for="item in allList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
        ></el-option>
    </el-select>
</template>

<script>
export default {
    data() {
        return {
            // 渲染列表,限制在20個(gè)以內(nèi)
            list: [],
            // 請(qǐng)求獲取的列表,數(shù)據(jù)量過(guò)大時(shí)不能直接渲染,會(huì)卡頓
            allList: [
                {
                    value: "選項(xiàng)1",
                    label: "黃金糕"
                },
                {
                    value: "選項(xiàng)2",
                    label: "雙皮奶"
                },
                {
                    value: "選項(xiàng)3",
                    label: "蚵仔煎"
                },
                {
                    value: "選項(xiàng)4",
                    label: "龍須面"
                },
                {
                    value: "選項(xiàng)5",
                    label: "北京烤鴨"
                }
            ],
            value: ""
        };
    },
    created() {
        // 模擬某些情況,需要設(shè)置默認(rèn)的下拉選項(xiàng),防止默認(rèn)選中值顯示不正常(例如:不顯示label,而顯示value)
        this.list = [{
                    value: "選項(xiàng)4",
                    label: "龍須面"
                },
                {
                    value: "選項(xiàng)5",
                    label: "北京烤鴨"
                }]
    },
    methods: {
        filterList(query = "") {
            let arr = this.allList.filter(item => {
                return item.label.includes(query) || item.value.includes(query);
            });
            if (arr.length > 20) {
                this.list = arr.slice(0, 20);
            } else {
                this.list = arr;
            }
        }
    }
};
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基于Vue的一些資料 內(nèi)容 UI組件 開(kāi)發(fā)框架 實(shí)用庫(kù) 服務(wù)端 輔助工具 應(yīng)用實(shí)例 Demo示例 element★...
    嘗了又嘗閱讀 1,282評(píng)論 0 1
  • 簡(jiǎn)說(shuō)Vue (組件庫(kù)) https://github.com/ElemeFE/element" 餓了么出品的VUE...
    Estrus丶閱讀 1,908評(píng)論 0 1
  • 主要還是自己看的,所有內(nèi)容來(lái)自官方文檔。 介紹 Vue.js 是什么 Vue (讀音 /vju?/,類(lèi)似于 vie...
    Leonzai閱讀 3,537評(píng)論 0 25
  • UI組件 element - 餓了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的組...
    魯大師666閱讀 43,624評(píng)論 5 97
  • 我懷疑我的想象力不夠了(*/?\*) 天太冷,其實(shí)我真實(shí)的字不是這樣的!這是瑟瑟發(fā)抖時(shí)寫(xiě)的(?ω?)
    Mia_張亞妮閱讀 272評(píng)論 2 2

友情鏈接更多精彩內(nèi)容