[Vuejs]-How to transfer data between components (siblings) Vue?

0๐Ÿ‘

You can emit an event and let the parent component handle the fetching and pass the data as props to the child component.

Or another way is to directly listen to the event as follows

Component 1:

this.$root.$emit('clickPrices', data);

Component 2:

mounted() {
    this.$root.$on('clickPrices', data => {
        this.getPricing();
    });
}

Check out the answer by @alex

0๐Ÿ‘

Since you are using 2 independent components (one is not included in the other), you cannot pass props

Things you can do โ€“

  1. Instead of fetching all the prices on click, just create a created hook like so, which will fetch all the pricing details whenever your component is created โ€“

    created () {
        this.getPricing()
    },
    
    methods: {
        getPricing() {
            axios.get('api/pricing').then((response) => {
                //some actions
                router.push('prices');
            });
        },
    },
    
  2. Use State and when a user clicks on the button, pricing details are fetched and added in the state. And you can just use the state anywhere in your application like so โ€“

    this.$store.state.prices

Let me know if it works, if not we will find some other solution for you!

Leave a comment