[Vuejs]-Is it possible to remove a prefers-color-scheme event listener in Vue?

3👍

Your code is not working because you are adding and removing in different MediaQueryList objects.

...
  mounted () {
    this.mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)')
    this.$nextTick(() => {
      this.mediaQueryList.addEventListener('change', this.setDarkMode)
    })
  },

  beforeDestroy () {
    this.mediaQueryList.removeEventListener('change', this.setDarkMode)
  }
...

JSFiddle

Leave a comment