在Vue中动态获取窗口高度可以通过使用JavaScript内置的window对象。可以在组件的mounted钩子函数中调用window.innerHeight来获取窗口高度。
下面是一个示例:
<template>
<p>窗口高度为{{height}}px</p>
</div>
</template>
<script>
export default {
data() {
return {
height: 0
mounted() {
this.height = window.innerHeight
</script>
如果您需要在窗口大小发生变化时动态更新高度,可以在mounted钩子函数中监听resize事件:
<template>
<p>窗口高度为{{height}}px</p>
</div>
</template>
<script>
export default {
data() {
return {
height: 0
mounted() {
this.height = window.innerHeight
window.addEventListener('resize', this.handleResize)
beforeDestroy() {
window.removeEventListener('resize', this.handleResize)
methods: {
handleResize() {
this.height = window.innerHeight
</script>