Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a Onload event with Vuejs - specifically how to put the script in the method and the way to get a call in the html

export default {

    methods: {
    hotbod() { document.querySelector("body")},

    doStuff() {
      hotbod.className += " animate";
    },


    window:onload = function() {
      doStuff();
    },
      },
    }

Above is the code, that I think has some errors in it - and for some reason I cannot get the javascript to work (On page load, there are CSS that gets loaded).

like image 443
Shadowed Zoum Avatar asked Oct 23 '25 01:10

Shadowed Zoum


1 Answers

To launch a component method on the window's load, simply add the load listener in your mounted() hook. For example:

mounted(){
    window.addEventListener("load", () => this.processLoad());
}
like image 91
Michael Sinclair Avatar answered Oct 24 '25 14:10

Michael Sinclair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!