[英]Vue returning data objects from axios call to controller
I'm using vue in laravel and trying to get a controller function that I'm hitting to return the data so that I can use it in the data()
section of my vue template. 我在laravel中使用vue,并尝试获取要返回的数据的控制器函数,以便可以在vue模板的
data()
部分中使用它。
I know the controller function returns what I need, but I'm not so sure how I need to handle the return/response in the axios call in order to start placing the data into the data() function in vue 我知道控制器函数返回了我需要的东西,但是我不确定如何在axios调用中处理返回/响应才能开始将数据放入vue中的data()函数中。
Blade/Vue template 刀片/ Vue模板
import moment from 'moment'
export default {
name: 'calendar',
data () {
return {
events: [
{
title: 'test',
allDay: true,
start: '2019-08-17',
},
],
config: {
defaultView: 'month',
eventRender: function(event, element) {
console.log(event)
}
},
}
},
created() {
this.fetchTasks();
},
methods: {
fetchTasks() {
axios.get('/landing/tasks' )
.then((response) => {
// handle success
this.assetOptions = response.data;
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
});
}
}
}
Route 路线
Route::get('/landing/tasks', 'landingController@getTasks')
->name('landing/tasks');
Controller 调节器
public function getTasks()
{
$getTask = Task::getTaskForLanding();
$result = array();
foreach($getTask as $id => $task){
$result[$task->taskt_id][] = $task;
}
}
If you are certain that the Controller returns what you need, the only thing you are missing is declaration of assetOptions
. 如果您确定Controller返回了您所需要的内容,那么唯一缺少的就是
assetOptions
声明的assetOptions
。 To be able to assign response.data
to assetOptions
later on, you have to declare it in the data
function first. 为了稍后能够将
response.data
分配给assetOptions
,您必须首先在data
函数中对其进行声明。
data() {
return {
...
assetOptions = []; // assuming you are expecting an array
...
};
}
Once that is done, you are all set. 一旦完成,就一切就绪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.