Di halaman utama, saya mempunyai bekas yang memaparkan beberapa elemen dan di bawahnya penomboran dengan semua halaman (1, 2, 3, 4...), penomboran berfungsi. Walau bagaimanapun, saya ingin menambah pertanyaan pada URL saya, jadi jika saya memasukkan sesuatu seperti http://localhost:8080/twity/?page=10 saya mahu pergi ke halaman itu. Saya menambah pertanyaan seperti ini tetapi ia hanya menunjukkan kepada saya nombor halaman semasa yang saya gunakan, jika saya menukar nombor dalam url ia hanya mengembalikan saya ke halaman pertama (lalai)...
this.$router.push({ query: { page: this.currentPage } })
Saya boleh berkongsi keseluruhan kod komponen Rumah saya jika perlu. Terima kasih.
Komponen halaman utama
<template> <div class="main"> <div class="tweets-container"> <div v-for="tweet in tweets" :key="tweet.id" > <div class="tweet-card"> <div class="username-time"> <div class="user-info"> <p class="name"> {{ tweet.twitter_name }} </p> <p class="user-info-p"> @ </p> <p class="username"> <a :href="'https://twitter.com/' + tweet.twitter_username" class="twitter_link" target="_blank" > {{ tweet.twitter_username }} </a> </p> </div> <div class="time"> <p>{{ tweet.added_at }}</p> </div> </div> <div class="text"> <p>{{ tweet.tweet_text }}</p> </div> </div> </div> </div> <div class="reply-container"> <h1>Replies</h1> </div> <ul class="pagination"> <li class="page-item"> <p class="hover-underline-animation" @click="previousPage" > Previous </p> </li> <li class="page-item all-pages"> <div v-for="page in numberOfPages" :key="page" :class="page == currentPage ? 'active' : 'hover-underline-animation'" @click="getTweets(page)" > <p>{{ page }}</p> </div> </li> <li class="page-item"> <p class="hover-underline-animation" @click="nextPage" > Next </p> </li> </ul> </div> </template> <script> import axios from 'axios' import { getUserToken } from '@/auth/auth' import moment from 'moment' export default { components: {}, data() { return { tweets: [], currentPage: 1, numberOfPages: 0, } }, beforeMount() { this.getTweets() }, methods: { nextPage() { this.currentPage += 1 this.tweets = [] this.getTweets() }, previousPage() { this.currentPage -= 1 this.tweets = [] this.getTweets() }, getTweets(newCurrent) { this.tweets = [] const API_URL = `${this.$server}/api/twitter/tweets` const params = { token: getUserToken(), page: this.currentPage, newCurrentPage: newCurrent, } axios.post(API_URL, null, { params }).then(res => { this.currentPage = res.data.page this.numberOfPages = res.data.numberOfPages // Set query string this.$router.push({ query: { page: this.currentPage } }) res.data.tweets.forEach(tweet => { const tweetData = { id: tweet.id, tweet_text: tweet.tweet_text, twitter_name: tweet.twitter_name, twitter_username: tweet.twitter_username, added_at: moment(String(tweet.added_at)).format('MM/DD/YYYY hh:mm'), } this.tweets.push(tweetData) }) }) }, }, } </script>
Fungsi nodejs (digunakan untuk mendapatkan data daripada pangkalan data dan mengawal paging)
getAllTweets: async (req) => { // get number of all rows in table let tweetsNum = await SQL('SELECT COUNT(*) as total FROM twitter_tweets') tweetsNum = tweetsNum[0].total // number of tweets const pageSize = 25 let skip = 0 const numberOfPages = Math.ceil(tweetsNum / pageSize) // page number from client const newCurrentPage = req.query.newCurrentPage let currentPage = req.query.page // check if there is newCurrentPage if(newCurrentPage != undefined) { skip = (newCurrentPage - 1) * pageSize currentPage = newCurrentPage } else { // check if page is valid) if(currentPage < 1) { currentPage = 1 } else if(currentPage > numberOfPages) { currentPage = numberOfPages } // offset skip = (currentPage - 1) * pageSize } // paginate tweets from db const tweetsFromDb = await SQL('SELECT * FROM twitter_tweets ORDER BY added_at DESC LIMIT ?,?', [skip, pageSize]) let tweets = Object.values(JSON.parse(JSON.stringify(tweetsFromDb))) const data = { tweets: tweets, page: parseInt(currentPage), numberOfPages: parseInt(numberOfPages), } return data }