【发布时间】:2021-02-26 18:13:59
【问题描述】:
我正在尝试使用 API 从数据库中获取数据,但我的 vue 控制器上没有输出。
我这样做对吗?
我认为我分配 scheduleList 的方式有误。
我对 vue.js 和 API 很陌生,我想知道我在这里做错了什么。
控制器
public function schedules(){
return Schedule::all();
}
api.php
Route::get('schedules', 'CalendarController@schedules');
Vue 组件
<script>
import axios from 'axios'
export default {
data() {
return {
schedules: [],
scheduleList: [
{
id: schedules.id,
title: schedules.title,
category: schedules.category,
start: schedules.start,
end: schedules.end
},
],
};
},
methods: {
loadSchedules() {
axios.get('/api/schedules')
.then((response) => {
this.schedules = response.data;
})
}
},
mounted() {
this.loadSchedules();
}
};
</script>
<style>
</style>
【问题讨论】:
-
您如何显示该列表?
-
我认为这是因为您正在为
scheduleList分配尚不存在的值 -
我正在使用 tui.calendar。但我认为问题在于如何分配值 id:schedule.id。这是正确的方法吗?
-
我收到错误 ReferenceError: schedules is not defined
-
将您的 loadSchedules 添加到 created() 中,然后将 async await 添加到您的 loadSchedules 函数中。
标签: laravel api vue.js vue-component