Problem when returning data through $ emit

0

I have 2 components, the child component generates an issue to the parent component and after the function is executed in the parent component that returns the data to the child component, what I have done is the following:

In the son component I make:

let data =  this.$emit('getData', this.search, this.pageSize);

and in the parent component I have the returns:

 getData(search, pageSize) {
         return axios.get(url, {
              params: {
                   search: search,
                   pageSize: pageSize
             }
         }).then(response => {
             his.users = response.data.data.data;

             return response.data.data;
           });
     },

which I do a return of what the call axios returns me, but I do not receive that data in the component where I generate the $ emit

Update

Next I will add the two components. What I need to return is the data of the pagination returned by Laravel: {page_current, total_pages}, which I receive with axios, but I need to pass it to the other component after the $ emit is executed, thus setting the attributes that I initialized with vue is pagination: {from, to, total}. which in turn these are used by the pagination component passed as props.

The parent component has:

<template>
    <div class="well dt">
        <datatables :headers="headers" :items="items" @getData="getData">

            <tbody>
            <tr v-for="user in users" :key="user.id" class="text-center">
                <td>
                    {{user.first_name}}
                </td>
                <td>
                    {{user.email}}
                </td>
                <td>
                    -
                </td>
            </tr>
            </tbody>
        </datatables>
    </div>

</template>


    <script>
        import datatables from '../tables/datatables';

        export default {
            name: "userdraw",
            data() {
                return {
                    headers: [
                        {label: 'Usuario', name: 'first_name'},
                        {label: 'Email', name: 'email'},
                        {label: 'Acción', name: ''}
                    ],
                    // tableData: {},
                    users: [],
                    items: [5, 25, 50],
                    search: ''
                }
            },
            components: {
                datatables: datatables
            },

            methods: {
                getData(search, pageSize) {
                    return axios.get('/FinDraT/user/draw/' + window.location.pathname.split('/')[4] + '/getRegisteredUsers', {
                        params: {
                            search: search,
                            pageSize: pageSize
                        }
                    }).then(response => {
                        this.users = response.data.data.data;

                        return response.data.data;
                    });

                    // + pageSize + '/' + pageNumber + '?query=' + filter
                },
            }
        }

    </script>

and the child component:

<template>

    <div>
        <div class="row filters">
            <div class="col-md-6 col-sm-12 mb-3">
                <span>Mostrar </span>
                <select class="form-control v-items" v-model="pageSize" @change="getData">
                    <option v-for="item in items">{{item}}</option>
                </select>
                <span> registros</span>
            </div>
            <div class="col-md-6 col-sm-12  mb-3">
                <div class="input-search pull-right">
                    <div class="input-group">
                        <input type="text" class="form-control" @keyup="getData" v-model="search"
                               :placeholder="placeholder">
                        <div class="input-group-append">
                    <span class="input-group-text" id="basic-addon2">
                       <i class="far fa-search"></i>
                    </span>
                        </div>
                    </div>
                </div>

            </div>
        </div>
        <table class="table table-bordered">
            <thead>
            <tr>
                <th v-for="header in headers" class="text-center">
                    {{header.label}}
                </th>
            </tr>
            </thead>
            <slot></slot>
        </table>
        <pagination :pagination="pagination"></pagination>
    </div>

</template>

<script>
    import pagination from './pagination';

    export default {
        name: "datatable",
        components: {
            pagination: pagination
        },
        mounted() {
            this.getData().then(data => {
                console.log( + data);
            });
        },
        watch:{

        },
        data() {
            return {
                search: '',
                pageSize: this.items[0],
                pagination: {
                    from: 0,
                    to: 0,
                    total: 0
                }
            }
        },
        props: {
            headers: '',
            placeholder: {
                type: String,
                default: 'Buscar...'
            },
            items: {
                type: Array,
                default() {
                    return [10, 25, 100, 'Todos']
                }
            }
        },
        methods: {
            getData() {

                    let items = this.$emit('getData', this.search, this.pageSize);

                });

            }
        }
    }

</script>
    
asked by Juan Pablo B 23.08.2018 в 17:49
source

1 answer

1

In order for your child to receive the information back, you need to go back to the indicated "prop".

The return of your function, does not return anything to anyone, because the emit only triggers the event, but does not expect an answer on it.

For this, your function is correct with respect to your call axios, but should fill items again this.items = response.data.data . You can delete the return quietly.

But, the son is not attentive to the reactive changes of the father. To be attentive we have to add a watch. We add this in the son.

A watch is nothing more than a function that is attentive to reactive changes in values.

watch: {
  items: function (newItems, oldItems) {
      this.items = newItems;
  }
},

In this way, we will be attentive to changes in items.

    
answered by 23.08.2018 / 19:35
source