Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
Trigger Window Resize Event Listener from another Component - Vuejs

I have created two Components Component1 and Component2. In the Component1 I have registered window resize event (Whenever the page is resizing, it is getting triggered). I want to invoke this resize event from Component2.

Component1:

export default {
    name: 'Component1',
    async mounted() {
       window.addEventListener('resize', this.windowResize);
    },
    methods: {
       windowResize() {
          console.log("Window Resize event called")
       }
    }
};

Component2:

export default {
    name: 'Component2',
    methods: {
       doResize() {
         // Need to invoke Component1 Resize from here
       }
    }
};

Note: These are not connected through the root vue instance.

I have tried the following but not worked,

Component1:

mounted() {
    this.$root.$on('Component1', () => {
        // your code goes here
        this.windowResize()
    }
}

Component2:

...
doResize(){
 this.$root.$emit('component1') //like this
},

Kindly help me to figure out this.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

methods: {
  dispatchWindowResize() {
    window.dispatchEvent(new Event('resize'))
  }
}

Calling this.dispatchWindowResize() inside that component will dispatch a new window.resize event on window object, which will run all listeners added to it from any source, including the ones added by other components in your app.

Documentation:

  • dispatchEvent
  • Event

Test:

const Emitter = Vue.defineComponent({
  template: '#emitter-template',
  methods: {
    dispatchWindowResize() {
      window.dispatchEvent(new Event('resize'))
    }
  }
})
const Receiver = Vue.defineComponent({
  template: '<div></div>',
  mounted() {
    window.addEventListener('resize', this.logResize)
  },
  methods: {
    logResize() {
      console.log(`window.resize: {w: ${window.innerWidth}, h: ${window.innerHeight}}`);
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.logResize)
  }
})

new Vue({
  el: '#app',
  components: { Emitter, Receiver }
})
<script src="https://v2.vuejs.org/js/vue.min.js"></script>
<div id="app">
  <receiver></receiver>
  <emitter></emitter>
</div>
<template id="emitter-template">
  <button @click="dispatchWindowResize">Dispath window resize</button>
</template>

<Receiver /> receives all resize events: the ones triggered by resizing the browser window and the ones triggered by clicking the button inside <Emitter />

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda