Ask Your Question
1

What is the method to retrieve the size of the window that is modified in Vue JS?

asked 2023-06-08 08:17:29 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-08 08:35:02 +0000

david gravatar image

In Vue JS, the window size can be retrieved using the following method:

  1. Create a data property to hold the window size.

    data() {
     return {
       windowSize: {
         width: window.innerWidth,
         height: window.innerHeight
       }
     };
    }
    
  2. 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
       };
     }
    }
    
  3. 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>
    
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-08 08:17:29 +0000

Seen: 19 times

Last updated: Jun 08 '23