vue虛擬列表解決數(shù)據(jù)量大導(dǎo)致的頁面卡頓

主要是解決下拉框數(shù)據(jù)量大(幾千條)導(dǎo)致的頁面渲染卡頓。

原理:虛擬列表,每次只渲染20條(可設(shè)置)條數(shù)據(jù)。

https://www.cnblogs.com/yuwenjing0727/p/18129844

使用步驟:

1、安裝插件 npm install vue-virtual-scroll-list --save
2、引入組件virtualSelect,放置在components里面
3、局部注冊組件或全局注冊組件 virtualSelect
4、頁面使用

// virtualSelect替換el-select
<virtualSelect
  style="width: 300px"
  v-model="form.yp"
  :selectData="{
    data: ypList, // 下拉列表的數(shù)組
    key: 'id',// 下拉框需要顯示的key
    label: 'customerName',// 下拉框需要顯示的名稱
    value: 'customerId',// 下拉框綁定的值
    right:  'customerId',// 右側(cè)顯示綁定的值
    isRight: true,//右側(cè)是否顯示 不顯示填false
  }"
  :multiple="true"
  placeholder="請選擇一級渠道"
  clearable
></virtualSelect>

<!-- <el-select
  v-model="form.yp"
  filterable
  multiple
  remote
  :loading="ypLoading"
  :remote-method="ypRemoteMethod"
  style="width: 300px"
>
  <el-option
    v-for="item in ypList"
    :key="item.id"
    :label="item.customerId + '/' + item.customerName"
    :value="item.customerId"
  ></el-option>
</el-select> -->

組件自定義設(shè)置:virtualSelect文件夾里面的index.vue

<virtual-list ref="virtualList"
  class="virtualselect-list"
  :data-key="selectData.value"
  :data-sources="selectArr"
  :data-component="itemComponent"
  :keeps="20"
  :extra-props="{
      label: selectData.label,
      value: selectData.value,
      optionKey: selectData.key,
      rightValue: selectData.right,
      isRight: selectData.isRight
  }"
></virtual-list>
<!-- 可設(shè)置屬性  -->
  data-key  String|Function  從data-sources每個數(shù)據(jù)對象中獲取唯一鍵。或每個函數(shù)都調(diào)用data-source并返回其唯一鍵。在中,其值必須唯一data-sources,用于標(biāo)識商品尺寸。
  data-sources  Array[Object]  列表數(shù)據(jù),每一行都必須有一個唯一的id(data-key)
  data-component Component 每一行的子組件
  keeps Number 默認(rèn)30個,默認(rèn)渲染的個數(shù)
  extra-props Object 默認(rèn){} data-component組件的額外props通過改屬性傳入,內(nèi)部已有source和index兩個
<!-- 常用方法 -->
可以通過以下方式調(diào)用這些方法ref:
  reset()  將所有狀態(tài)重置為初始狀態(tài)。
  scrollToIndex(index)  手動設(shè)置滾動位置到指定索引。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

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

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