添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Monitoring changing window sizes in a Vue.js application is crucial for creating responsive designs. Vue provides a few ways to achieve this, allowing you to dynamically respond to changes in the window dimensions. In this article, we will learn How to monitor changing window sizes in Vue.

Two common approaches involve using the window object directly or utilizing the Vue resize event.

Table of Content

  • Using window Object Directly
  • Define a custom Vue directive
  • Approach 1: Using window Object Directly

    In this approach, we will directly listen to the resize event on the window object. This is a straightforward method, but it involves adding a global event listener.

    Syntax:

    window.addEventListener('resize', callbackFunctionMonitorChanges);

    Example: The below wexample will explain how to use window object to detect the window size change.

    // the component is destroyed
    window.removeEventListener
    ('resize', this.handleWindowSizeChange);
    methods: {
    handleWindowSizeChange() {
    // Update data properties with
    // new window dimensions
    this.windowWidth = window.innerWidth;
    this.windowHeight = window.innerHeight;
    // Additional logic based on
    // window size changes
    console.log('Window dimensions changed:',
    this.windowWidth,
    this.windowHeight);
    template: `
    < h2 style = "color:green" >
    GeeksforGeeks
    How to monitor changing
    window sizes in Vue?
    Window Width: {{ windowWidth }}px
    Window Height: {{ windowHeight }}px
    </ div >
    </ script >
    </ body >
    </ html >

    Approach 2: Define a custom Vue directive

    In this approach, we will define a custom vue directive to monitor changes. The bind hook will be used to attach a window resize event listener. This approach allows you to apply the v-resize directive to any element and customize the behavior in the associated method. We define a custom Vue directive named resize. The directive calls the specified method (handleResize in this case) with the current width and height of the element whenever a resize occurs. The unbind hook removes the event listener when the element is unbound.

    Example: The below code example explains the use of the custom vue directive to monitor changes in the window sizes.

    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
    You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.