Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
VueJS weird $emit behaviour

I am experimenting a really weird behaviour from my Vue app.

I have the following two components inside one of my router views:

<bootstrap-table @showcurrent-click="handleShowcurrent"
                 @edit-click="handleEdit" 
                 @delete-click="handleDelete"
                 @generated-id="retrieveID"
                 :columns="columns" 
                 :data="data" 
                 :options="options"></bootstrap-table>

<device-showcurrent :device="showCurrent" 
                    :show="showShowCurrentModal"
                    @modal-close="handleHideShowcurrentModal"></device-showcurrent>

The problem

Inside my device-showcurrent component, which is a Bootstrap modal, I handle the hide.bs.modal event with JQuery and emit a custom modal-close event so I can handle the event from the router view component (you can see in the previous piece of code the @modal-close="handleHideShowcurrentModal") like this:

mounted: function(){
        // Due to Javascript scope we assign this to a variable
        instance = this
        // This ID is generated on creation, and it works corretly.
        // I also checked that this JQuery event was triggering.
        $(`#${this.randomID}`).on('hide.bs.modal', e => {
            instance.$emit('modal-close')
        })
    }

But the problem is that the event is not emitting from device-modal, but from bootstrap-table (checked through VueDevTools), so the first piece of code I posted does not work, but the following does:

<bootstrap-table @showcurrent-click="handleShowcurrent"
                         @edit-click="handleEdit" 
                         @delete-click="handleDelete"
                         @generated-id="retrieveID"
                         @modal-close="handleHideShowcurrentModal"
                         :title="$t('message.audited_devices')" 
                         :columns="columns" 
                         :data="data" 
                         :options="options"></bootstrap-table>

        <device-showcurrent :device="showCurrent" 
                            :show="showShowCurrentModal"></device-showcurrent>

The question

What is happening here? How is it possible that the custom event is being emitted from another component?

Thank you very much!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found the problem. I changed the mounted hook to:

mounted: function() {
        let _this = this;
        $(`#${this.randomID}`).on('hide.bs.modal', function(e) {
            _this.$emit('modal-close')
        })
    }

It was probably a scope problem. which has been solved by:

  1. Declaring the "instance copy" (_this) with let.
  2. Changing the callback function from the JQuery listener declaration from arrow function to a normal function.

It still feels quite strange because the event was being emitted, but that was probably because I also had an instance = this on my bootstrap-table component which was being globally scoped (Ì was not using let either).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda