简体   繁体   中英

How to send data from laravel controller to vuejs

How do I send data from Laravel Controller to vue? I have it set up how I thought it might work but no data is being sent. What have I done wrong? Controller(set in a separate API folder):

public function index()
    {
        return User::all();
    }

Route from api.php:

Route::apiResources([
    'user' => 'API\UsersController'
]);

Component code:

<template>
    <div>
        <v-toolbar flat color="white">
            <v-toolbar-title>Users</v-toolbar-title>
        </v-toolbar>
        <ol v-for="user in users">
            <li>Name: {{ user.name }}</li>
        </ol>
        <v-data-table
            :headers="headers"
            :items="users"
            :items-per-page="5"
            class="elevation-1"
            flat
        >
        </v-data-table>
    </div>
</template>

<script>
    import {AxiosInstance as axios} from "axios";

    export default {
        data () {
            return {
                headers: [
                    {
                        text: 'Username',
                        align: 'left',
                        value: 'username',
                    },
                    { text: 'Name', value: 'name' },
                    { text: 'Surname', value: 'surname' },
                    { text: 'Email', value: 'email' },

                ],
                users: [],
            }
        },
        methods: {
            loadUsers(){
                axios.get('./api/user').then(response => this.users = response.data);
            }
        },
        mounted() {
            this.loadUsers();
        }
    }
</script>

I just put a simple list up there to test the array but it just comes out blank.

replace this

 axios.get('./api/user').then(response => this.users = response.data);

with this

  axios.get("api/user").then(({data}) => (this.users = data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM