Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vuejs 3 – Unable to remove resizeEvent

I have resize event in my mounted() works well.

 data() {
    return {
      ...
      eventHandler: null,
    };
  },
  mounted() {
    this.eventHandler = window.addEventListener("resize", () => {
      console.log("resize");
    });
  },

but when I tried to remove the resize listener on unmount()

  beforeUnmount() {
    console.log("unmonunted");
    window.removeEventListener("resize", this.eventHandler);
  },

the resize event is still firing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Anyone know how to solve this?

>Solution :

window.addEventListener returns undefined, not the event handler function.

You should change your code like this:

  data() {
    return {};
  },
  mounted() {
    window.addEventListener("resize", this.eventHandler);
  },
  beforeUnmount() {
    window.removeEventListener("resize", this.eventHandler);
  },
  methods: {
    eventHandler() {
      console.log("resize");
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading