我有問題。當我點擊這個按鈕時,它會收到一個完整的數據庫,但我想要部分數據庫。我怎樣才能做到這一點?Laravel + Vue.js.加載更多的數據,當我點擊按鈕
例如:每次點擊後,我想閱讀10篇文章。 Thx尋求幫助。
Messages.vue:
<div class="chat__messages" ref="messages">
<chat-message v-for="message in messages" :key="message.id" :message="message"></chat-message>
<button class="btn btn-primary form-control loadmorebutton" @click="handleButton">Load more</button>
</div>
export default{
data(){
return {
messages: []
}
},
methods: {
removeMessage(id){...},
handleButton: function() {
axios.get('chat/messagesmore').then((response) => {
this.messages = response.data;
});
}
},
mounted(){
axios.get('chat/messages').then((response) => {
this.messages = response.data
});
Bus.$on('messages.added', (message) => {
this.messages.unshift(message);
//more code
}).$on('messages.removed', (message) => {
this.removeMessage(message.id);
});
}
}
控制器:
public function index()
{
$messages = Message::with('user')->latest()->limit(20)->get();
return response()->json($messages, 200);
}
public function loadmore()
{
$messages = Message::with('user')->latest()->get();
// $messages = Message::with('user')->latest()->paginate(10)->getCollection();
return response()->json($messages, 200);
}
PAGINATE(10)加載只有10個職位
嘗試此堆棧[答案](https://stackoverflow.com/questions/37343541/loading-data-when-button-is-clicked-using-vue -js-and-laravel) –