1 | initial version |
In Vue JS, the window size can be retrieved using the following method:
Create a data property to hold the window size.
data() {
return {
windowSize: {
width: window.innerWidth,
height: window.innerHeight
}
};
}
Add an event listener to the mounted hook to update the window size when it is resized.
mounted() {
window.addEventListener("resize", this.getWindowSize);
},
beforeDestroy() {
window.removeEventListener("resize", this.getWindowSize);
},
methods: {
getWindowSize() {
this.windowSize = {
width: window.innerWidth,
height: window.innerHeight
};
}
}
Use the windowSize
property wherever it is needed in the Vue component.
<template>
<div>
<p>Window Width: {{ windowSize.width }}</p>
<p>Window Height: {{ windowSize.height }}</p>
</div>
</template>