gpt4 book ai didi

vue.js - 试图在 Vue.js 中找到 div 的高度,我通过 refs 访问它时出错

转载 作者:行者123 更新时间:2023-12-05 03:51:44 25 4
gpt4 key购买 nike

我正在尝试在 Vuejs 中找到 div 的高度,

let height = this.$refs.infoBox.clientHeight()+'px';

它给我一个错误,说我找不到 null/undefined 的客户端高度。

最佳答案

如果你的 div 有一个引用附加到它,如:<div ref="infoBox" />您可以使用 this.$refs.infoBox.offsetHeight 访问该 div 的高度.

注意:只有组件安装后,您才能访问 div 的高度。这行不通:

{
// ...

created() {
// WARNING
// Will trigger an error because the DOM is not mounted at this point.
console.log(this.$refs.infoBox.offsetHeight);
},

// ...
}

关于vue.js - 试图在 Vue.js 中找到 div 的高度,我通过 refs 访问它时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62639050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com