best practice for passing data from one Vue.js component to another in Laravel

Viewed 534

I have two components of Vue.js. I created two separate components for a single form in order to avoid a messy and complex code structure. I want to combine the data of these two forms in order to send it to the server. Here is my blade file which contains two components. One is StudentComponent.vue and second is TeacherComponent.vue.

    @extends('layouts.app')
    @section('content')
    <div class="main-content" id="panel">
        <main class="col-12 col-md-8 col-xl-7 py-md-3 pl-md-5 ct-content" role="main">
            <div id="app">
                <teacher-component :foods_array="{{ json_encode($foods) }}"></teacher-component>
                <student-component :foods_array="{{ json_encode($foods) }}"></student-component>
            </div>
        </main>
    </div>
    @endsection

Remember that these components (StudentComponent and TeacherComponent) hierarchy does not have a parent-child component relation. My goal is to get the data from the teacher component to the student component. How can I achieve this goal?

My StudentComponent.vue

    <template>
        <div>
            <div class="card" style="width: 172%;">
                <!-- Form Fields -->
            </div>
        </div>
    </template>
    
    <script>
        export default {
            props : {
                foods_array: {
                    type: Array,
                    required: true
                }
            },
            beforeMount: function () {
                this.foods = this.foods.concat(this.foods_array); 
            },
            data() {
                return {
                    student: {
                        email: '',
                        name: '',
                        roll_number:null,
                        food: null,
                        checked: []
                    },
                    foods: [{ text: 'Select One', value: null }],
                    show: true
                }
            },
            methods: {
                onSubmit(evt) {
                    evt.preventDefault()
                    axios.post('api/food/store'{student:JSON.stringify(this.student),role:'student'})
                    .then(function (response) {
                        console.log(response);
                     })
                     .catch(function (error) {
                        console.log(error);
                     });
                },
             }
        }
   </script>
     

My TeacherComponent.vue

    <template>
        <div>
            <div class="card" style="width: 172%;">
                <!-- Form Fields -->
            </div>
        </div>
    </template>
    
    <script>
        export default {
            props : {
                foods_array: {
                    type: Array,
                    required: true
                }
            },
            beforeMount: function () {
                this.foods = this.foods.concat(this.foods_array); 
            },
            data() {
                return {
                    teacher: {
                        email: '',
                        name: '',
                        department:'',
                        food: null,
                        checked: []
                    },
                    foods: [{ text: 'Select One', value: null }],
                    show: true
                }
            },
            methods: {
                onSubmit(evt) {
                    evt.preventDefault()
                    axios.post('api/food/store'{student:JSON.stringify(this.student),role:'teacher'})
                    .then(function (response) {
                        console.log(response);
                     })
                     .catch(function (error) {
                        console.log(error);
                     });
                },
             }
        }
   </script>
1 Answers

Depending on complexity of your project you can go down two main routes


1. A Parent Component

Create a vue component to be the parent of your two other components. You can then emit from the two child components and handle them in the parent.

If you are dealing with a limited number of components and aren't reusing them much, this can be a viable option


2. VueX

For larger projects with many components spread over multiple files, it may be that you will need to add some dedicated state management.

VueX will do this for you but does add additional complexity.

https://vuex.vuejs.org/

Related