【发布时间】:2020-11-17 07:10:05
【问题描述】:
我是 vue laravel 的新手,正在尝试使用 json 响应从 API 控制器获取数据。我觉得在嵌套对象中获取数据并不是最佳实践。实现这一点的最佳方法是什么,以便我可以得到this.user_data.designations = All_Designation(with res.data) 和其他一些像this.user_data.employment = All_Employments 等。
当我尝试通过 props 发送数据时,我可以访问它
designations: this.data.map(d => ({label: d.designation_name, value: d.designation_name, id:d.id}))
我还想要其他数据,所以我想应该是 this.data.designations, this.data.employment。这让我很困惑。 我怎样才能在不改变特质的情况下管理所有事情?
这是我的控制器方法:
public function index()
{
$designations = Designation::all();
if (!$designations->isEmpty()) {
$this->responseData['response'] = 1;
$this->responseData['message'] = "Designations has been Recieved.";
$this->responseData['data'] = $designations;
$this->status = 200;
}
return $this->apiResponse();
}
API 特征:
protected $responseData = [
'response' => 0,
'message' => "Not Found",
'data' => null
];
/**
* @var int
*/
protected $status = 404;
/**
* @return mixed
*/
public function apiResponse()
{
return response()->json($this->responseData, $this->status);
}
Axios 调用:
this.$store.dispatch('employee/getDesignations' )
.then(res => { this.user_data.designations = res.data.data })
.catch(err => {
console.error(err)
})
【问题讨论】: