Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

javascript - 如何将数据从laravel控制器发送到vuejs(How to send data from laravel controller to vuejs)

How do I send data from Laravel Controller to vue?(如何将数据从Laravel Controller发送到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):(控制器(在单独的API文件夹中设置):)
public function index()
    {
        return User::all();
    }

Route from api.php:(从api.php路由:)

Route::apiResources([
    'user' => 'APIUsersController'
]);

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.(我只是在这里放了一个简单的列表来测试该数组,但是它只是空白。)

  ask by Matt B translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

replace this(取代这个)

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

with this(有了这个)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...