mounted() {
this.$refs.index.style.paddingBottom = this.$refs.sus.clientHeight + 'px'
}
试着在mounted声明周期去获取某元素的高度,但是仍然获取到的是0,即使是DOMContentLoad 或者 window.onload 后仍然取不到。
那vue要什么时候才能获取dom的实际计算结果?
mounted() {
this.$refs.index.style.paddingBottom = this.$refs.sus.clientHeight + 'px'
}
试着在mounted声明周期去获取某元素的高度,但是仍然获取到的是0,即使是DOMContentLoad 或者 window.onload 后仍然取不到。
那vue要什么时候才能获取dom的实际计算结果?
试试看this.$nextTick
mounted (){
this.$nextTick(() => {
this.$refs.index.style.paddingBottom = this.$refs.sus.clientHeight + 'px';
});
}
绑定对应的事件就可以
methods:{
this.$nextTick(() => {
this.$refs.index.style.paddingBottom = this.$refs.sus.clientHeight + 'px';
})
}
9 回答1.7k 阅读✓ 已解决
6 回答1.6k 阅读
3 回答1.4k 阅读✓ 已解决
4 回答1.3k 阅读✓ 已解决
3 回答1.1k 阅读
2 回答1.3k 阅读✓ 已解决
3 回答1.4k 阅读✓ 已解决
就是在mounted的时候,如果你要获取的元素高度是动态由数据撑起来的需要使用
this.$nextTick
可测试上方代码。