老白菜菜

导航

解决elemet ui表格自适应高度 但是切换路由报Cannot read property XX of undefined at windowonresize错误问题

/**
 * 自适应表格高度
 * @param that
 * @param num
 */
export function fitHeight(that, num) {
  that.$nextTick(function () {
    that.tableHeight = window.innerHeight - that.$refs.table.$el.offsetTop - num;

    // 监听窗口大小变化
    let self = that;
    window.onresize = function() {
      self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - num
    }
  })
  //this.$refs.table.$el.offsetTop:表格距离浏览器的高度
  //50表示你想要调整的表格距离底部的高度(你可以自己随意调整),因为我们一般都有放分页组件的,所以需要给它留一个高度 
}

上方时自适应的代码,关键点 window.onresize

 

跳转页面调整窗口之后报错:Cannot read property XX of undefined at window.onresize

 

解决方案:

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
  window.onresize = function() {
    // console.log('离开了')
  }
})

这就ok了

posted on 2021-02-03 11:31  老白菜菜  阅读(204)  评论(0编辑  收藏  举报