php - 将两个或多个变量从 laravel 控制器发送到 vue 组件
问题描述
假设我有一个像这样的 Vue 组件
export default {
created() {
axios.get("a url").then(res => {
console.log(res.data);
});
}
};
然后 axios 在 laravel 控制器中向这个函数发送请求
public function something()
{
$data = Model::all();
$comments = Comment::all();
// here i want to send both $data and $comments to that view
return response()->json();
}
我可以把它们都寄出去吗?这个组件和函数只是一个例子
解决方案
在这里你可以创建一个关联数组并通过 json 发送。
public function something()
{
$data=Model::all();
$comments=Comment::all()
return response()->json([
'data'=>$data,
'comments'=>$comments
]) //here i want to send both $data and $comments to that view
}
在 Vue 中你可以写这样的东西。
export default
{
data(){
return {
comments:[]
}
},
created()
{
axios.get("a url")
.then(res=>{
this.comments = [...res.data.comments] //If you are using ES6
}
}
}
推荐阅读
- php - PHP:使用其他包含文件中的变量来更改 HTML 文本
- python - 将字符串联合起来用作可能的字典键
- laravel - 将数据从视图传递到视图并在视图 laravel 中显示?
- android - 正则表达式在不同的系统上有不同的形式吗?
- mysql - 我正在尝试选择所有 actor_id 为素数的演员。数字 2 和 3 也是质数。除了我的查询还有其他方法吗?
- suitescript2.0 - 如何为 map reduce CSV 文件或 JSON 数据编写 SuiteScript 作为输入过程并创建客户记录
- http - 如何在chainlink中发送带有json正文请求的http post?
- bash - ZAP API 扫描失败并出现错误读取超时
- treeview - 如何通过鼠标更改treview数据窗口中的默认蓝色选择点头
- sql - 一个选择不适用于另一个,oracle db