Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between Vue siblings

After researching this question thoroughly and after implementing the Bus method to communicate between siblings, I am getting a webpack error, so either i'm not implementing right (using latest CLI as of yesterday), or I need some other method.

I am new to Vue, coming from React and I have built a simple app wrapping 2 components in app.vue:

an input that sends data:

<GoogleInput @searchEvent="getSearchLocation($event)"></GoogleInput>

and a map container that should receive the data:

<GoogleMapsContainer :location="location" name="location-project"></GoogleMapsContainer>

I successfully implemented child (GoogleInput) to parent communication in app.js via:

getSearchLocation(input) {
  this.location = input;
}

with a method in GoogleInput:

this.$emit('searchEvent', ev.target.value);

Up to here everything was smooth.


Yet when trying to communicate my input value through to sibling (GoogleMapsContainer) via the bus method:

In my entry index.js: const Bus = new Vue({});

A new emit in my sending component:

Bus.$emit('passLocation', {location: this.location})

and in my receiving component:

Bus.$on('passLocation', (input) => { this.location = input; });

I get a webpack error:

Error in created hook: "TypeError: __WEBPACK_IMPORTED_MODULE_1_vue__.$on is not a function"

I am looking for "the shortest distance" to communicate my input to map container, without going into solving webpack issues (if this is a webpack issue at all, or just a fat finger mistake).

BTW: If vuex isn't a time consuming method to implement (as React-Redux is) in here, that would be a cool route as well, but I must keep this design (already in Git)

Thanks

like image 252
clusterBuddy Avatar asked Apr 23 '18 19:04

clusterBuddy


People also ask

How do you pass data between sibling components in Vue?

To communicate between sibling components in Vue. js, we can emit event to the root component of the app with this. $root. $emit .

How do you pass data between child components?

By adding the state isOpen to the parent, we can facilitate communication between the two sibling components. When the Button component is clicked it emits an event that updates the isOpen variable. That variable is then passed down to the Toggle component as a prop.

How do you pass data between sibling components in ReactJS?

To move data from a component to a sibling component, the simplest way is to pass a function from the parent component as a prop (i.e. "prop function") to its child that wants to update the data.


1 Answers

You probably have some import errors.

In your index.js, export it like:

export const Bus = new Vue({});

And in the files you do Bus.$emit(...) or Bus.$on(...) import it like:

import { Bus } from './index'; // make sure you use the correct relative path
like image 86
acdcjunior Avatar answered Dec 12 '22 10:12

acdcjunior