Laravel + Vue.js does not pass variables to component

2

I am working on this project with Laravel and Vue.js . I need to show a table with a lot information. For this I decided to use a Vue component that renders a table and the paging buttons with little information.

This works very well in the theory, because in practice the variables that happened to the instance of the component are not obtained correctly (or I do not know how to do it).

I have three fundamental parts if I'm not mistaken:

The app.js file that contains the main instance and the API call:

Vue.component('activities-table', require('./components/ActivitiesTableComponent.vue'));
...
const app = new Vue({
    el: '#app',
    data: {
        ...
        activities: [],
        ...
    },
    mounted: function() {
        ...
    },
    created: function() {
        ...
        this.loadActivities();
        ...
    },
    methods: {
        ...
        loadActivities: function(page = 1) {
            axios.get('/api/admin/actividades?page=' + page)
                .then(function (response) {
                    app.activities = response.data;
                });
        },
        ...
    },
    ...
});

I think it is important to emphasize that the call to the API is done correctly and that the information returned is a JSON file, from the contractor. This one has a very simple method:

public function getActivities(Request $request)
{
    $activities = Activity::orderBy('created_at', 'desc')->paginate(100);

    return $activities;
}

This returns a LengthAwarePaginator object with the following information:

That translated to JSON is:

  

Note: This can also be seen in the Vue developer tool where activities has the correct information.

Taking a big leap of information, we render the table with the element <activities-table :rows="this.activities"></activities-table> and then in it we define:

<template>
    <!-- Actividades -->
    <table id="activities" class="table table-bordered table-striped" width="100%">
        ...
    </table>
    <!-- /Actividades -->

    <!-- Paginación -->
    <div class="row d-flex justify-content-center pt-4">
        ...
    </div>
    <!-- /Paginación -->
</template>

<script>
    module.exports = {
        props: [ 'rows' ],
        data: function() {
            return {
                data: this.rows
            }
        },
        created: function() {

        },
        methods: {
            paginate: function(page) {
            }
        }
    }
</script>

When I do console.log(this.rows) in the cycle created or mounted I get the following information:

[

Why does not this work?

    
asked by Maramal 02.03.2018 в 22:06
source

2 answers

0

You are complicating yourself too much.
If you are using vue only as a component using the blade views, what you should do is:

In the View

<activities-table :rows="'{{$activities}}'"></activities-table>

On the controller rendering the view

return view('nombre_de_la_vista', ['data_vista' => $data, 'activities' => $activities]);
    
answered by 10.03.2018 в 11:00
0

Complementing the answer suggested by Maru Amallo I suggest you send the data as json and that vue be responsible for parsearla ...

<activities-table :rows="{{json_encode($activities)}}"></activities-table>
    
answered by 08.05.2018 в 14:04