1

I have checked similar question, but there is still one thing that is unclear to me:

Can I pass a parameter in emit on event hub, but I need parameter to be VALUE and not the VARIABLE which stores value. So for example: eventHub.$emit('test_emit', true) and the method which is called on test_emit should have it's parameter set on true.

2
  • Yes, you can, but what have you tried? Commented Jan 18, 2019 at 21:18
  • Exactly that, the method I am trying to call should have it's parameter set on true only when called through event hub and in all other cases it should be false. But I am not sure how to get that parameter, when I pass it as value. On the other side I've got eventHub.$on('test_emit', this.functionINeed) Commented Jan 18, 2019 at 21:22

2 Answers 2

2

From the similar question that you provided, you would just replace name with true when you are emitting event

methods: {
    showModal(name) { this.bus.$emit('showModal', true); },
}

created() {
    // `show` will have the value that you emitted
    this.bus.$on('showModal', (show) => console.log(show);
}
Sign up to request clarification or add additional context in comments.

4 Comments

And that true value would be written in show variable?
One more thing, how would look the part with eventHub.$off? Do I need to include same variable there or not?
No, you just specify the event name that you want to disable. documentation for $off. So this.bus.$off('showModal') is enough
Because you can add multiple listeners for the same event, this.bus.$off('showModal') will remove all those listeners for showModal event
1

Sure you can, what you cannot do is to pass more then one variable (like eventHub.$emit('test_emit', true, false) as $emit accepts only one additional parameter (that can be the value or an object containing the key: value associations, also know as payload.

2 Comments

Okay and how to you get it on the other side, where you haveeventHub.$on?
eventHub.$on('test_emit', theValue => ...); or if you provide a function, the function first parameter will be the value itself, like: eventHub.$on('test_emit', anyFn); anyFn = function(value) { console.log(value) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.