我需要從 Vue3 中的 URL 獲取選舉 id,以便在稍后的呼叫中將其用作 axios 呼叫中的查詢引數。我的網址是“http://localhost:8080/#/requestPIN/py6lzerjwjntl4dmr9n77e6x73blk925?electionId=thirdElection”
我正在嘗試做:
get ID(): string | undefined {
return this.$route.query.electionId
}
這給了我一個打字稿錯誤:
Type 'LocationQueryValue | LocationQueryValue[]' is not assignable to type 'string | undefined'. Type 'null' is not assignable to type 'string | undefined'.
URL 查詢引數的正確型別是什么?
uj5u.com熱心網友回復:
斷言 id 是一個字串,然后在回傳型別中確保說它可能undefined仍然回傳。
get ID(): string | undefined {
return this.$route.query.electionId as string;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/528611.html
上一篇:如何在標頭中發送http請求?
