我已經為此作業了大約2個小時,但無濟于事。我已經搜索了互聯網,嘗試創建條件等。
所以我在 VueJS 組件中有一個方法,如下所示:
paginate() {
const test = this.$route.query.page;
console.log(test);
this.currPage = parseInt(test);
}
最后一行的單詞“test”有一個紅色下劃線,我收到一條錯誤訊息:
常量測驗:LocationQueryValue | 位置查詢值[]
'LocationQueryValue | 型別的引數 LocationQueryValue[]' 不可分配給“字串”型別的引數。
“null”型別不能分配給“string”型別。Vetur(2345)
我應該如何在不編輯 TypeScript 組態檔的情況下解決 TypeScript 錯誤?
uj5u.com熱心網友回復:
這意味著test可以null。它的型別很可能string | null是聯合型別。您需要對其進行測驗以消除這種null情況。
例如
paginate() {
const test = this.$route.query.page;
console.log(test);
if (typeof test === 'string') {
// test has type string here because of the predicate
this.currPage = parseInt(test);
}
}
游樂場鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/480682.html
