【问题标题】:Getting data from Laravel Api to Vue Page从 Laravel Api 获取数据到 Vue Page
【发布时间】:2019-04-24 12:08:47
【问题描述】:

我是 vue.js 的初学者,我正在尝试获取 api 结果并将其显示到我的 vue 页面。我的 Api 是用 Laravel 5.7 构建的。 我刚刚安装了 Vue axios 包来使用 http。

这是我的代码:

任务控制器

 public function index()
 {
    return response(Task::all()->jsonSerialize(), Response::HTTP_OK);
 }

App.vue

<template>
<div class="app-component">
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Task Title</th>
                <th>Priority</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <task-component v-for="task in tasks" :key="task.id" :task="task"></task-component>
           <tr>
               <td><input type="text" id="task" class="form-control"></td>
               <td>
                   <select id="select" class="form-control">
                       <option>Low</option>
                       <option>Medium</option>
                       <option>High</option>
                   </select>
                </td>
                <td><button class="btn btn-primary">Add</button></td>
            </tr>
        </tbody>
    </table>
</div>
</template>

<script>
import TaskComponent from './Task.vue';
export default{
    data(){
        return{
                tasks: [],
        }
    },

    methods: {
        getTasks(){
            window.axios.get('/api/tasks').then(({data})=>{
                data.forEach(task =>{
                this.tasks.push(task)
            });
          });
        },
        created(){
            this.getTasks();
        }
    },
    components:{TaskComponent}
}
</script>

任务页面

<template>
     <tr>
        <td>{{ task.id }}</td>
        <td>{{ task.title }}</td>
        <td>{{ task.priority }}</td>
        <td><button class="btn btn-danger">Remove</button></td>
    </tr>
</template>
<script>
export default{
    data(){
        return{
        }
    },
    props: ['task']
}   

</script>

虽然控制器正确返回 json 数据,但我的 vue 没有出现任何错误

【问题讨论】:

    标签: vuejs2 axios laravel-5.7


    【解决方案1】:

    created() 钩子不应该在方法中:

    export default {
       methods: {},
       created() {}
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2019-01-28
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2018-08-26
      • 2018-05-18
      • 2019-01-18
      • 2020-02-16
      相关资源
      最近更新 更多