首页 > 其他分享 >优化el-popover在列表等地方使用时,会大量渲染,造成页面首次加载卡顿、加载时间长的问题

优化el-popover在列表等地方使用时,会大量渲染,造成页面首次加载卡顿、加载时间长的问题

时间:2024-10-16 10:47:39浏览次数:1  
标签:el default type refs popover document true 加载

vue2项目中,在列表等需要循环渲染的地方,使用el-popover时,数据量大了以后,会造成页面卡顿。

解决方案:基于el-popver二次封装

 <template>
    <div class="my-popover-container">
        <span ref="referenceRef" class="comp-reference" @click="triggerPop" @mouseenter="mouseEnter" @mouseleave="mouseLeave">
            <slot name="referenceContent"></slot>
        </span>
        <el-popover
            v-show="showPop"
            ref="hasClosePopoverRef"
            v-bind="$attrs"
            :popper-class="`self-class-close-btn-popover ${popoverClassName}`"
            trigger="manual"
            :append-to-body="true"
            :width="width"
            :placement="placement"
            :reference="reference"
            @show="showPopover"
            @after-leave="hide"
        >
            <div ref="popoverContentRef" class="popover-content">
                <div v-if="title || showClose" class="content-top">
                    <span class="title">{{ title }}</span>
                    <i v-if="showClose" class="el-icon-close" @click="close"></i>
                </div>
                <div v-if="showAfterLoading ? show : true" class="content-center" :style="{ 'max-height': maxContentHeight, ...contentCenterStyle }">
                    <slot></slot>
                </div>
            </div>
        </el-popover>
    </div>
</template>

<script>
export default {
    name: 'MyPopover',
    props: {
        width: {
            type: [Number, String],
            default: 150
        },
        placement: {
            type: String,
            default: 'top-start'
        },
        // 触发方式hover/click
        trigger: {
            type: String,
            default: 'click'
        },
        title: {
            type: String,
            default: ''
        },
        // 内容最大高度, 超出滚动
        maxContentHeight: {
            type: String,
            default: 'auto'
        },
        // 是否需要popover显示后再加载内容
        showAfterLoading: {
            type: Boolean,
            default: false
        },
        // 是否显示关闭按钮
        showClose: {
            type: Boolean,
            default: true
        },
        contentCenterStyle: {
            type: Object,
            default: () => {}
        },
        // 点击弹窗内容,是否关闭弹窗
        isClickClose: {
            type: Boolean,
            default: false
        },
        // popper-class其他类名
        popoverClassName: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            show: false,
            showPop: false,
            reference: null,
            timeOut: null
        }
    },
    watch: {
        showPop(val) {
            this.$emit('show-pop', val)
            if (!val) {
                document.removeEventListener('click', this.handleDocumentClick, true)
                document.removeEventListener('scroll', this.updatePopover, true)
                document.removeEventListener('mousemove', this.handleDocumentMousemove, true)
            }
        }
    },
    beforeDestroy() {
        document.removeEventListener('click', this.handleDocumentClick, true)
        document.removeEventListener('scroll', this.updatePopover, true)
        document.removeEventListener('mousemove', this.handleDocumentMousemove, true)
    },
    methods: {
        mouseEnter() {
            if (this.trigger !== 'hover') return
            this.reference = this.$refs.referenceRef
            this.showPop = true
            this.$nextTick(() => {
                document.addEventListener('scroll', this.updatePopover, true) // 解决popover不跟随触发元素滚动问题
                this.$refs.hasClosePopoverRef.doShow()
                this.$nextTick(() => {
                    this.$refs.hasClosePopoverRef.updatePopper()
                })
            })
        },
        mouseLeave() {
            if (this.trigger !== 'hover') return
            this.$nextTick(() => {
                document.addEventListener('mousemove', this.handleDocumentMousemove, true)
            })
        },
        triggerPop() {
            if (this.trigger !== 'click') return
            this.reference = this.$refs.referenceRef
            // 类似el-select的展开收起效果,需要transition="el-zoom-in-top"
            if (this.isClickClose) {
                if (this.showPop) {
                    this.close()
                    return
                }
            }
            this.showPop = true
            this.$nextTick(() => {
                document.addEventListener('click', this.handleDocumentClick, true)
                document.addEventListener('scroll', this.updatePopover, true) // 解决popover不跟随触发元素滚动问题
                this.$refs.hasClosePopoverRef.doShow()
                this.$nextTick(() => {
                    this.$refs.hasClosePopoverRef.updatePopper()
                })
            })
        },
        close() {
            this.$refs?.hasClosePopoverRef?.doClose()
            this.showPop = false
        },
        showPopover() {
            this.show = true
            this.$nextTick(() => {
                this.$refs.hasClosePopoverRef.updatePopper()
            })
        },
        hide() {
            this.show = false
        },
        handleDocumentClick(e) {
            if (!this.isClickClose) {
                if (!this.$refs.popoverContentRef.contains(e.target)) {
                    this.close()
                }
            } else {
                if (!this.$refs.referenceRef.contains(e.target)) {
                    this.close()
                }
            }
        },
        handleDocumentMousemove(e) {
            clearTimeout(this.timeOut)
            this.timeOut = setTimeout(() => {
                if (!this.$refs?.hasClosePopoverRef?.$refs?.popper.contains(e.target) && !this.$refs.referenceRef.contains(e.target)) {
                    this.close()
                }
            }, 20)
        },
        updatePopover() {
            this.$refs.hasClosePopoverRef && this.$refs.hasClosePopoverRef.updatePopper()
        }
    }
}
</script>
<style lang="scss" scoped>
.my-popover-container {
    display: inline-block;
    line-height: 0;
    .comp-reference {
        display: inline-block;
        line-height: 0;
    }
}
</style>
<style lang="scss">
.self-class-close-btn-popover {
    min-width: 100px;
    padding: 0;
    font-family: PingFangSC, PingFang SC;
    &.popover-out-container {
        box-shadow: 0px 3px 14px 6px rgba(0, 0, 0, 0.05), 0px 8px 10px 1px rgba(0, 0, 0, 0.06), 0px 5px 5px -3px rgba(0, 0, 0, 0.1);
        border-radius: 4px;
        border: 1px solid #dcdcdc;
        &.el-popper[x-placement^='bottom'] {
            margin-top: 8px;
        }
    }
    &.not-arrow-b-mt4 {
        &.el-popper[x-placement^='bottom'] {
            margin-top: 4px;
        }
    }
    .popover-content {
        .content-top {
            display: flex;
            height: 51px;
            padding: 0 16px;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #f2f3f5;
            .title {
                font-weight: 500;
                font-size: 14px;
                color: #333333;
                line-height: 22px;
            }
            .el-icon-close {
                font-size: 16px;
                color: #666666;
                cursor: pointer;
                &:hover {
                    color: #db9130;
                }
            }
        }
        .content-center {
            margin: 16px 0;
            padding: 0 16px;
            overflow-y: auto;
        }
    }
}
</style>

组件使用

<my-popover
    width="240"
    placement="bottom-start"
    popover-class-name="popover-out-container"
    transition="el-zoom-in-top"
    :show-close="false"
    :is-click-close="true"
    :content-center-style="{ margin: '4px 0', padding: 0 }"
    @show-pop="cutFlowShow"
>
        <div>这里是内容</div>
    <span slot="referenceContent">触发</span>
</my-popover>

 

标签:el,default,type,refs,popover,document,true,加载
From: https://www.cnblogs.com/hong1/p/18469330

相关文章

  • python+eel入门示例
    安装eelpipinstalleelpyimporteelimportrandom#笑话列表jokes=["为什么电脑经常生病?因为窗户(Windows)总是开着!","为什么数学书看起来总是很悲伤?因为它里面有太多的问题(problems)","为什么海洋里没有电脑?因为它们总是遇到短路(seals)","为什么冰......
  • elasticsearch之倒排索引
    倒排索引elasticsearch有如此高的搜索性能,无异于使用了倒排索引。倒排索引中有两个重要的概念:文档(Document):用来搜索的数据,其中的每一条数据就是一个文档。例如一个网页、一个商品信息词条(Term):对文档数据或用户搜索数据,利用某种算法分词,得到的具备含义的词语就是词条。例如:我......
  • ELK实时监控Nginx日志
    ELK分析Nginx日志和可视化展示一、概述使用ELK收集nginxaccess日志,利用Grafana做出一套可视化图表二、环境准备环境说明操作系统:centos7.6docker版本:19.03.12ip地址:192.168.31.196elk搭建关于elk的搭建,请参考以下3篇文章:docker安装elasticsearch和head插件docker安......
  • excel戴你玩转
    自定义排序条件筛选冻结窗口右下角查看引用单元格CTRL+SHIFT+下(同时选中所有列)调整列宽去重函数=UNIQUE(B2:B883)sumif/sumifs,输入条件要放在“内容”,按住ctrl不用输入<,>=SUMIFS(C:C,A:A,">=2020/8/3",B:B,H2)=sumifs(C:C,A:A,">="&A261,B:B,H2)拖拽变化......
  • python pandas写入excel
    #--coding:utf-8--importdatetimeimportpandasaspdfromcommon_toolimportget_ip_areafromdb.mysqlConnectionimportMyPymysqlPoolfromdb_configimportdata_report_dbsex_dict={"-1":"未知","0":"女&......
  • vue 动态加载路由,渲染左侧菜单栏
    需求我们在route文件中定义的路由是由子路由包裹进去的,它可能是无限级的。如何在vue的模板中渲染形成菜单栏。如图: 解决方法将菜单栏单独写成子组件(注意头部标签:element-plus中是el-menu)仍然在父组件中。将配置路由数据传入到子组件。子组件渲染一级路由。一级路由再调用......
  • 小白也能学会的预测新模型!ReliefF特征选择+XGBoost回归!
    小白也能学会的预测新模型!ReliefF特征选择+XGBoost回归!目录小白也能学会的预测新模型!ReliefF特征选择+XGBoost回归!预测效果基本介绍程序设计参考资料预测效果基本介绍Matlab实现ReliefF-XGBoost多变量回归预测1.excel数据集,7个输入特征,1个输出特征。2.......
  • 基于常青藤算法优化深度混合核极限学习机(IVY-DHKELM)的数据多变量回归预测 Matlab (
    [原创]基于常青藤算法优化深度混合核极限学习机(IVY-DHKELM)的数据多变量回归预测Matlab(多输入单输出)程序已经调试好,无需更改代码替换数据集即可运行!!!数据格式为excel!①将多项式核函数与高斯核函数加权结合,构造出新的混合核函数,并引入自动编码器对极限学习机进行改进,建......
  • 基于深度混合核极限学习机DHKELM的数据多特征分类预测 Matlab (多输入单输出)
    基于深度混合核极限学习机DHKELM的数据多特征分类预测Matlab(多输入单输出)程序已经调试好,无需更改代码替换数据集即可运行!!!数据格式为excel!①将多项式核函数与高斯核函数加权结合,构造出新的混合核函数,并引入自动编码器对极限学习机进行改进,建立DHKELM模型。该想法创新性......
  • Selenium操作:测试form表单
    from表单是经常测试的用例,用户登录、注册等都会用到form表单,本文简单设计了一个用户登录的form表单,并对该form表单进行测试一、自定义form表单1、用到的组件如下图,图中定义了一个登录界面的form表单,用到的表单元素:type="text";type="submit"2、代码示例新建HTML文件文......