How to share data between components in VueJS

daniels picture daniels · Oct 24, 2016 · Viewed 23.9k times · Source

I have a fairly simple VueJS app, 3 components (Login, SelectSomething, DoStuff)

Login component is just a form for user and pass input while the second component needs to display some data obtained in the login progress.

How can I share data from one component to the others? So that when I route to second component I still have the data obtained in the Login one?

Answer

highFlyingSmurfs picture highFlyingSmurfs · Oct 25, 2016

You can either use props or an event bus, where you'll be able to emit an event from a component and listen on another

vm.$on('test', function (msg) {
  console.log(msg)
})

vm.$emit('test', 'hi')
// -> "hi"