[Vuejs]-I want to Pass data from component to js file(not in other component) in vue js. I am able to access it in component but not in js file

0👍

You are using the event hub pattern correctly, so your code works.

CodeSandbox Demo here.

Some points may be preventing you from seeing the listener ($on) working:

  • The listener is declared in abc.js. Simply declaring it does not guarantee that it is executed. Make sure abc.js is imported somewhere so the code executes and the listener is properly registered.
  • The $emit event code is triggered by a method that is on a v-on:change. For <input>s, the change event happens when the inputs lose focus, not on typing. The event that happens char by char is input (or the key* events, naturally). So type some text and focus out the input to see the event triggered.
  • If your question code is just a demo and the code that triggers the event is another .js file, make sure it executes after the abc.js does.

The demo above handles these cases.

Leave a comment