【发布时间】:2022-01-17 03:46:36
【问题描述】:
我有一个 Vue 组件,它从后端加载书籍的数据,然后放入卡片中。
我想让用户将书籍的排序从默认更改为按评级或最新等,所以我声明了一个变量名称sorting 并设置为默认值并添加一个下拉菜单来更改变量从默认排序,并将变量放在从后端获取数据的 URL 中,如 get_books 方法:
data(){
return {
audio_books:[],
page:1,
last_page:false,
sorting:"default",
}
},
methods: {
get_books() {
axios.get('load_all_audio_books/'+this.sorting+'?page='+this.page).then(response=> {
$.each(response.data.data, (key, v) => {
this.audio_books.push(v);
if (response.data.current_page==response.data.last_page){
this.last_page=true;
}
});
})
this.page++;
},
后端:- web.php
Route::get('/load_all_audio_books/{sort}',[\App\Http\Controllers\load_book::class,'get_all_audiobooks']);
后端函数:-
public function get_all_audiobooks($sort)
{
if($sort=="default")
{
$books=DB::table('audio_books')->paginate(10);
return $books;
}
elseif ($sort=="rate")
{
$books=DB::table('audio_books')->orderBy('rating','desc')->paginate(10);
return $books;
}
elseif ($sort=="newest")
{
$books=DB::table('audio_books')->orderBy('created_at','desc')->paginate(10);
return $books;
}
elseif ($sort=="oldest")
{
$books=DB::table('audio_books')->orderBy('rating')->paginate(10);
return $books;
}
现在我想要的是在变量排序发生变化时重新渲染组件。
【问题讨论】:
标签: javascript vue.js vue-component