实现思路:如图,获取h总、h1、h2,则table高度就为h总-(h1+h2)。
1.定义一个属性存放计算后的table高度
tableHeight:10,
2.给最外层dom添加ref="container"
,给table添加ref="orderTable"
,并将上面定义的table高度绑定到table上:height="tableHeight"
2.定义函数
calHeight() {
this.$nextTick(() => {
//获取外层元素
const rect = this.$refs.container.getBoundingClientRect()
//计算高度
this.tableHeight = rect.height-300
//对table进行重新布局
this.$refs.orderTable.doLayout();
console.log('this.tableHeight',this.tableHeight)
})
},
4.在mounted()
、beforeUpdate()
、beforeDestroy()
中调用
mounted(){
window.addEventListener('resize', this.calHeight)
},
beforeUpdate() {
this.calHeight()
},
beforeDestroy() {
window.removeEventListener('resize', this.calHeight)
},