前提:后端返回一個幾千條的數組,頁面直接渲染導致非常卡頓。
解決方案:使用vue-virtual-scroller 插件解決;https://www.npmjs.com/package/vue-virtual-scroller#page-mode
第一步:安裝
npm install --save vue-virtual-scroller
第二步:引入(也可以局部引入)
import "vue-virtual-scroller/dist/vue-virtual-scroller.css"; import virtualScroller from "vue-virtual-scroller"; Vue.use(virtualScroller);
第三步:頁面使用

<!-- 明細列表部分 --> <div class="list_count" style="height:400px;"> <RecycleScroller v-if="invoiceListflag" :items="invoiceList" :emitUpdate="true" class="scroller" :item-size="49" key-field="idKey" > <template slot="before"> <div class="thead"> <div class="tr"> <span class="td" v-for="(itemsa,indexsa) in tableTitle" :key="indexsa+'title'" :class="{'width200':indexsa ==1|| indexsa ==2}">{{itemsa.title}}</span> </div> </div> </template> <template v-slot="{ item,index }"> <div class="tbody"> <div class="tr" :key="index+'asdsds'"> <span class="td">{{item.id}}</span> <span class="td width200"> <el-select v-model="item.invoiceType" style="width:156px;" placeholder="請選擇" @change="changeType(item,item.id,1)"> <span slot="prefix" :class="{'zhuan':item.invoiceType == '00','pu':item.invoiceType =='02'}"></span> <el-option v-for="items in options" :key="items.value" :label="items.invoiceTypeName" :value="items.invoiceType"> </el-option> </el-select> </span> <span class="td width200"> <el-select v-model="item.goodsName" style="width:156px;" placeholder="請選擇" @change="changeType(item,item.id,2)"> <el-option v-for="items in opParams[item.invoiceType]" :key="items.goodsName+'asd'+Math.random()" :label="items.goodsName" :value="items.goodsName"> </el-option> </el-select> </span> <span class="td">{{item.invoiceEditionDesc}}</span> <span class="td">{{item.goodsQuantity}}</span> <span class="td">{{item.goodsPrice}}</span> <span class="td">{{item.goodsAmount}}</span> <span class="td">{{item.taxRate}}%</span> <span class="td">{{item.goodsTax}}</span> <span class="td" > <el-input-number class="inputWidth" :class="{'errorColor':changeErrorColor(item)}" :min="0" controls-position="right" v-model="item.taxAmount" @change="checkPrice(item,item.id)" placeholder="請輸入金額"></el-input-number> <span v-if="changeErrorColor(item)"></span> </span> <span class="td"><span v-if="invoiceList.length>1" class="del" @click="delList(item,item.id)">刪除</span></span> </div> </div> </template> <!-- </tbody> </table> --> </RecycleScroller> </div>

<style lang='css'> .scroller { height: 100%; } .user { height: 32%; padding: 0 12px; display: flex; align-items: center; } .invoiceAdd__detail_warp .list_count .vue-recycle-scroller__item-wrapper { overflow: initial; } .invoiceAdd__detail_warp .list_count .vue-recycle-scroller.ready.direction-vertical .vue-recycle-scroller__item-view { width: inherit; } </style>
備注:
1:父元素一定要設置高度;
2:key-field="idKey"//key-field 默認是id屬性這里一定要傳遞一個不重復的值進行區分;
3::item-size="49" //是每一條數據的高度
4:items //數組列表