vue中如何获取容器的宽度
可以使用ref来获取容器的DOM元素,然后通过DOM API获取宽度。
示例代码:
<template>
<div ref="container">
...
</div>
</template>
<script>
export default {
mounted() {
const containerWidth = this.$refs.container.offsetWidth;
console.log(containerWidth);
}
}
</script>
在mounted钩子函数中,通过this.$refs.container获取容器的DOM元素,然后使用offsetWidth获取宽度。
原文地址: https://www.cveoy.top/t/topic/bqCX 著作权归作者所有。请勿转载和采集!