laravel - laravel中的分页vue数据
问题描述
我试图在vue.js
我找到它的包(gilbitron/laravel-vue-pagination)中对我的数据进行分页工作并安装它,但它不起作用,我想我在文档中做了所有事情,但不知何故它不起作用,
controller
$projects = Project::orderby('id', 'desc')->paginate(10);
vue template
<pagination :data="projects" @pagination-change-page="load"></pagination>
component script
import pagination from 'laravel-vue-pagination'; //added here
export default {
data() {
return {
projects: []
}
},
mounted: function() {
this.load()
},
methods: {
load: function(page = 1) { //added here
axios.get('/api/projects?page=' + page) //added here
.then(
response => {
this.projects = (response.data);
}
)
.catch(function (error) {
this.errors.push(error);
console.log(error);
})
}
}
}
错误
[Vue warn]: Unknown custom element: <pagination> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
问题
- 我做错什么了吗?如何解决这个问题?
更新
controller
public function index()
{
$projects = Project::orderby('id', 'desc')->paginate(10);
return $projects->map(function($project) {
$data = collect($project);
$data->put('owner', $project->user);
return $data->all();
});
}
PS:我没有返回我的数据的原因如下:
return response()->json([
"projects" => $projects,
], 200);
是将项目用户添加到结果中。
更新 2
我在我的控制器和脚本中做了一些更改,所以我现在可以将我的数据作为 json 而不是数组,但我仍然无法获得分页:
我的数据现在的样子:
{
"projects":{
"current_page":1,
"data":[
{
"id":38,"
user_id":1,
"title":"wwwwwwwwwwwwwwwwwwwww",
"slug":"wwwwwwwwwwwwwwwwwwwww",
"body":"wwwwwwwwwwwwwwww",
"created_at":"2018-08-10 15:55:28",
"updated_at":"2018-08-10 15:55:28",
"user":{
"id":1,
"name":"admin",
"email":"admin@admin.com",
"created_at":"2018-08-02 10:57:47",
"updated_at":"2018-08-02 10:57:47",
"profile":{
"id":1,"
user_id":1,"
photo":"project-attachment-1533159133.png",
"created_at":"2018-08-02 10:57:48",
"updated_at":"2018-08-02 10:57:48"
}
}
},
]
}
}
controller
public function index()
{
$projects = Project::orderby('id', 'desc')->with('user')->with('user.profile')->paginate(10);
return response()->json([
"projects" => $projects,
], 200);
}
component
<script>
import pagination from 'laravel-vue-pagination';
export default {
data() {
return {
projects: []
}
},
mounted: function() {
this.load()
},
components: {
pagination
},
methods: {
load: function(page = 1) {
axios.get('/api/projects?page=' + page)
.then(
response => {
this.projects = (response.data.projects.data); //here has changed
}
)
.catch(function (error) {
this.errors.push(error);
console.log(error);
})
}
}
}
</script>
解决方案
需要注册导入的分页组件app.js
import pagination from 'laravel-vue-pagination';
Vue.component('pagination', pagination);
推荐阅读
- python - 使用 loc 提取字符串
- css - Css 未加载到 node.js 生成的链接上
- python - 有没有办法为 FastAPI 中的选定请求参数生成请求模型?
- android - Android - Firebase Analytics 不记录我的事件
- ios - iOS 14 UIDatePicker - 允许用户清除值
- shell - 在 GNU find 中打印没有小数部分的秒数
- python - VSC Python:调试时出现anaconda问题
- delphi - Delphi dbGrid - 列名
- python - Optuna 多目标优化的最佳参数
- python - 使用 gekko 的约束多变量非线性回归