Reputation: 93
I have one parent component with 2 child components. In ChildComponent1 I use axios to get my data and list it with v-for ( Cars to rent ). Every block of data has an button which upon click, it should select that vehicle. The selected vehicles data should transfer to the ChildComponent2 which is a Booking summary card, or panel so to speak. Link:
https://codesandbox.io/s/koyjjoo1y3
I'm not sure what approach to take here. I used props to pass response data from parent to child. But I have no clue how can I pass that data from 1 child to another on button click.
Result.vue / Parent
<section class="section__results--cars">
<div class="col-lg-8">
<cars-details></cars-details>
</div>
<div class="col-lg-4">
<booking-summary></booking-summary>
</div>
</section>
CarsDetails.vue / Child1
<v-card class="mb-3" v-for="car in cars" :key="car.id">
<img :src="car.image"></img>
<h4 class="title">{{ car.title }}</h4>
<car-attributes
:maxPersons="car.maxPersons"
:maxLuggage="car.maxLuggage"
:interiorColor="car.interiorColor"
:exteriorColor="car.exteriorColor"
></car-attributes>
<div>
<span class="price__title">Hourly price</span>
<span class="price__value">{{ car.pricePerHour }}€</span>
<v-btn @click="passData">Select Car</v-btn>
</div>
<div>
<h6 class="subheading font-weight-medium">Description</h6>
<p>{{ car.description }}</p>
</div>
</v-card>
<script>
import axios from "axios";
const URL = "https://next.json-generator.com/api/json/get/NJgWweNm8";
import CarAttributes from "@/components/cars/CarAttributes";
export default {
name: "carsdetails",
components: { CarAttributes },
data: () => ({
cars: []
}),
mounted() {
axios
.get(URL)
.then(response => {
this.cars = response.data;
})
.catch(e => {
this.errors.push(e);
});
}
};
</script>
BookingSummary.vue / Child2
<v-card>
<h4>Booking Summary</h4>
<h6>{{ car.title }}</h6>
<h6>{{ car.maxPersons}}</h6>
</v-card>
Upvotes: 3
Views: 1980
Reputation: 5553
The main design pattern is Data -> Down and Events <- Up. Use $emit('some-event', payload)
to push the event up the chain, and use `@some-event="yourFunction" to handle it.
Here is an article that explains why this pattern works (and works long term): https://jasonformat.com/props-down-events-up/
For your use case
Parent
<template>
<div>
<cars-details
v-for="(car, i) in cars"
:key="i"
:car="car"
@getCar="getCar"
/>
</div>
<div v-if="selectedCar">
<booking-summary :car="selectedCar" />
</div>
</template>
<script>
export default {
data: {
cars: [car1, car2, ...],
selectedCar: null
},
methods: {
getCar(car) {
this.selectedCar = car
// Do axios call for the `car` object
}
}
}
</script>
Child1
<template>
...
<div>
...
<v-btn @click="passData">Select Car</v-btn>
</div>
</template>
<script>
export default {
...
props: {
car: { required: true }
},
methods: {
passData() {
this.$emit('getCar', this.car)
}
}
}
</script>
Child2
<template>
...
</template>
<script>
export default {
...
props: {
car: { required: true }
},
}
</script>
Upvotes: 1
Reputation: 2930
You need to use vueJs eventBus to communicate with the components across the project.
It is simple to use and declare.
// EventBus.js file
import Vue from 'vue';
const EventBus = new Vue();
export default EventBus;
//CarsDetails.vue component (create event and pass data through it)
import EventBus from "../../EventBus";
export default {
name: "carsdetails",
data: () => ({
isClicked: true,
cars: []
}),
methods: {
ok(car) {
console.log(car);
EventBus.$emit("finished", car);
}
}
};
//BookingSummary.vue component (listen to the event)
import EventBus from "../../EventBus";
export default {
name: "bookingsummary",
data() {
return {
car: null
};
},
created() {
EventBus.$on("finished", x => {
this.car = x;
});
console.log(this.car);
}
};
Here is the updated code https://codesandbox.io/s/n42j172jol
Upvotes: 1
Reputation: 24311
You should use $emit to raise an event, and then listen for that event in the other component.
Upvotes: 1