我通過引數獲取屬性資料。在此屬性資料中是所定位的代理的 ID。因此,在成功獲取屬性資料后,我想通過剛剛收到的 ID 獲取代理資料。問題是,如果我手動重繪 頁面,代理資料不會被獲取。如果我通過應用導航進入頁面,它會。
這是為什么?還是我必須更改獲取資料的結構中的某些內容?
async fetch() {
try {
const property = await this.$axios.$get(
`/api/get-specific-property/${this.$route.params.id}`
)
if (property.success) {
this.property = property.data
const agentIDs = property.data.agents
this.getAgents(agentIDs)
}
} catch (err) {
console.log(err)
}
},
methods: {
async getAgents(agentIDs) {
try {
const agents = await this.$axios.$post('/api/get-specific-agents', agentIDs)
if(agents.success) {
this.agents = agents.data
}
} catch(err) {
console.log(err)
}
}
}
uj5u.com熱心網友回復:
您可能應該使用以下方法等待您的方法
await this.getAgents(agentIDs)
其余的看起來不錯!
uj5u.com熱心網友回復:
使用 SSRfetch()默認在服務器端運行
- 如果您指定
fetchOnServer: false,那么它只發生在客戶端
@PhilippeUn 使用async/await語法而不是try/catch. 但肯定更容易解決 fetch 掛鉤中的兩個 API 呼叫。(然后總是你可以使用this.$fetch()它,它會重新運行鉤子。)
它允許使用
$fetchState.error/pending狀態只是提示停止使用
console.log錯誤,嘗試使用console.error,因為它會跟蹤錯誤。
但我主要擔心的是使用 $POST 方法來獲取資料?使用 GET 請求資料
我知道通過 POST 您可以在請求中使用 BODY ,但這會給后端帶來很多風險。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/516269.html
標籤:javascriptVue.jsVuejs2nuxt.js
