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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With