Skip to content

Get return value (int) of $emit  #5702

@ian-patel

Description

@ian-patel

Version

2.3.3

Reproduction link

https://jsfiddle.net/ianpatel/3bs145u7/

Steps to reproduce

var eventHub = new Vue()

Vue.component('foo', {
	template: `<div>I'm foo <button @click="emit">Emit {{ value }}</button></div>`,
  data: function() {
  	return {
	  	value: 22
    }
  },
  methods: {
  	emit() {
    	var newVal = eventHub.$emit('makeTwice', this.value)
      // I'm expecting newVal will be 44 but it is object of Vue$3
      alert(newVal)
     // Please check into Console log
      console.log(newVal)
      
      
      this.value = newVal // Check console log, can not Convert
    }
  }
})

new Vue({
  el: '#app',
  data: function() {
  	return {}
  },
  mounted() {
  	eventHub.$on('makeTwice', this.makeTwice)
  },
  methods: {
  	makeTwice(val) {
            return val * 2
    	}
  }
})

What is expected?

I'm trying to Call other component method using single event hub var eventHub = new Vue(), but I would like to get a return value from other component method.
https://vuejs.org/v2/guide/migration.html#dispatch-and-broadcast-replaced

What is actually happening?

Currently eventHub.$emit('makeTwice', this.value) returning object of Vue$3. but component method return a value.

Vue$3 {_uid: 0, _isVue: true, $options: Object, _renderProxy: Proxy, _self: Vue$3…}


Am I missing something in https://vuejs.org/v2/api/#vm-emit.
vm.$emit( event, […args] ) do not have any return; It may possible, I'm expecting return value but $emit do not return method's return value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions