vue 中用this.$refs获取dom元素高度,给eleme
2019-05-31 本文已影响0人
Mihansy
先设置元素的ref,如 <div style="height: 500px" ref="tablewrap" ></div>,要获取该元素高度,必须先给该元素设置css高度
再用 window.getComputedStyle(this.$refs.tablewrap).height 获取
注意:用this.$refs获取dom元素高度,必须在mounted钩子中,如下:
mounted() {
console.log(window.getComputedStyle(this.$refs.tablewrap).height)
}
给element表格设置动态高度,就可以用上述方法
![](https://img.haomeiwen.com/i9029365/90120ca16d9d3ba7.png)
在data中定义变量
![](https://img.haomeiwen.com/i9029365/9408edcdc1370ce7.png)
最后,在mounted钩子中设置表格的高度
![](https://img.haomeiwen.com/i9029365/a55f1ffa54cc1b4d.png)