Answer the question
In order to leave comments, you need to log in
How to organize the interaction of non-hierarchically connected components in vue2?
Hello everyone, please be as simple as possible.
How to organize interaction in a view of two components (files .vue)
1. Interaction parent - child is vague but I can imagine. BUT
2. What to do if there is no submission? How to make two components interact?
For example:
I have an App.vue that is the parent of all components included in it.
It includes three more components: StepOne.vue StepTwo.vue StepThree.vue
These components contain:
1. A list with a choice of values
2. Range sliders
3. Checkboxes
QUESTION ONE:
How can I make these components appear only after the user interacts with them in the last step? (ie 2 appears after the user has selected a value on the first one, etc.)
QUESTION TWO:
How do I pass values from one component to another? (let's say my slider values depend on the value selected in the first step). Google and the documentation prompts to look towards BUS but I do not quite understand.
Answer the question
In order to leave comments, you need to log in
In the case of not hierarchically related (not having a common parent, for example), you can use:
1) Common store (vuex)
2) Event Bus
In the case of single-file event bus components, this is just a js file with the content:
import Vue from 'vue';
export const EventBus = new Vue();
methods: {
addToCart: function() {
EventBus.$emit('add-to-cart', 1);
}
}
EventBus.$on("add-to-cart", count => {
console.log(`Cart updated on ${count} items.`)
})
<App>
<StepOne></StepOne>
<StepTwo v-if="isListItemSelected"></StepTwo>
<StepThree v-if="isRangeShowing"></StepThree>
</App>
<StepThree v-if="isRangeShowing" :range="rangeValues"></StepThree>
and don't forget to specify props: ['range'] in StepThree .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question