1.监听window窗口变化

VueJs 监听 window.resize 方法,同时窗口拉伸时会频繁触发resize函数,导致页面性能 卡顿 ,可以搭配setTimeout来提升性能

data() {
    return {
       screenWidth: document.body.clientWidth,//初始化宽度
    }
},

在mounted中挂载resize方法

var _this = this
window.onresize = () => {
        return (() => {
            _this .screenWidth = document.body.clientWidth
        })()
      }

watch 监听 data中或props传递的数据

screenWidth(){
        if (!this.timer) {
            this.timer = true
            let _this= this
            setTimeout(function () {
              ... (执行的语句)
              _this.timer = false
            }, 500)
        }
      }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐