此功能在按鈕單擊時觸發
nextPage() {
this.tweets = []
this.$route.query.page = 1
this.getTweets()
},
它正在做的是將推文陣列重置為零,將現有查詢(頁面)增加 1 并呼叫 getTweets 函式
getTweets(newCurrent) {
this.tweets = []
const API_URL = `${this.$server}/api/twitter/tweets`
const params = {
token: getUserToken(),
// THIS IS WHERE I AM PASSING QUERY
page: this.$route.query.page,
newCurrentPage: newCurrent,
}
axios.post(API_URL, null, { params }).then(res => {
this.currentPage = res.data.page
this.numberOfPages = res.data.numberOfPages
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)
})
})
}
最后在 nodejs 中。在這里我得到 typeof number 但 currentPage 仍然是連接的,如果我在第 20 頁并單擊下一步, currentPage 將是 201。如何避免這種情況?
var currentPage = parseInt(req.query.page)
console.log(typeof currentPage)
console.log(currentPage)
// 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 - 1
}
// offset
skip = (currentPage - 1) * pageSize
}
uj5u.com熱心網友回復:
vue-router 中的查詢引數始終被視為string. 因為它可能是page=1它可能的樣子page=foo,所以沒有辦法知道。
?page=1也會如此this.$route.query.page === "1"
要解決此問題,您必須先決議引數:
const newPage = parseInt(this.$route.query.page, 10) 1
旁注,分配一個新值$router.query.page不起作用。您必須推送一條新路線才能更新:
const newPage = parseInt(this.$route.query.page, 10) 1
this.$router.push({ query: newPage })
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/467483.html
