首先给表格初始一个高度
tableHeight: (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - 240
监听浏览器窗口变化
window.onresize = () => {
let tableHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
this.tableHeight = tableHeight - 240;
}
在watch中监听高度的变化
watch: {
// 这里的定时器是为了优化,如果频繁调用window.onresize方法会造成页面卡顿,增加定时器会避免频繁调用window.onresize方法
tableHeight(val) {
if (!this.timer) {
this.tableHeight = val
this.timer = true
const that = this
setTimeout(function() {
that.timer = false
}, 400)
}
}
}