IT Log

Record various IT issues and difficulties.

“How to Let Vue Know When the Page Has Been Loaded”


To make Vue aware of when a page has loaded, you can use either window.addEventListener(‘DOMContentLoaded’) or window.onload. Here’s how to implement it:

  1. Using DOMContentLoaded:
    if (typeof window !== ‘undefined’) {     window.addEventListener(‘DOMContentLoaded’, () => {       // Page is ready; initialize Vue here       new Vue({         // your options       });     });   }

  2. Using window.onload:
    if (typeof window !== ‘undefined’) {     window.onload = function() {       // Page is fully loaded; initialize Vue here       new Vue({         // your options       });     };   }

Option 1 is preferable as it fires earlier, making the page ready sooner.


, , , ,

5 responses to ““How to Let Vue Know When the Page Has Been Loaded””

  1. Thanks for sharing this useful tip! The comparison between DOMContentLoaded and window.onload is very informative.

  2. The code examples are straightforward and easy to follow. Perfect for someone new to Vue and JavaScript events!

  3. I appreciate the detailed breakdown of both methods. This will be helpful for developers working on complex Vue projects.

  4. Using DOMContentLoaded seems like a better choice for an optimal user experience. It’s good that the author highlighted this as the preferred method.

  5. This article provides clear and concise methods to let Vue know when the page is loaded. The two approaches are well-explained, making it easy to implement either one.

Leave a Reply