国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

錯(cuò)誤:''string | string'類型的參數(shù)不可分配給'string'類型的參數(shù)”
P粉232409069
P粉232409069 2023-08-25 11:44:28
0
1
862
<p>我正在開發(fā)一個(gè) Vue 項(xiàng)目,同時(shí)使用 TypeScript 和 axios 進(jìn)行 API 調(diào)用,同時(shí)開發(fā)重置密碼組件,我的 auth.ts 文件中的 resetPassword 函數(shù)如下所示</p> <pre class="brush:php;toolbar:false;">resetPassword(password1: string, password2: string, uid: string, token: string): Promise<any> { return new Promise<any>((resolve, reject) => { axios .post("API", { password1, password2, uid, token }) .then((res : any) => { resolve(//RESOLVE); }) .catch((error) => { //REJECT }) }) }</pre> <p>在我的 ResetPassword.vue 文件中,我使用如下所示的 newPassword</p> <pre class="brush:php;toolbar:false;">this.resetPassword(password1.value, password2.value, this.$route.params.id, this.$route.params.resetID).then( (res) => { if(password1.value == password2.value){ Notification{ "SUCCESS" }); }</pre> <p>在我的 ResetPassword.vue 文件中,第三個(gè)參數(shù)出現(xiàn)錯(cuò)誤,提示<strong>“類型‘string[]’不可分配給類型‘string’?!?lt;/strong></p> <p>我對 ts 有點(diǎn)陌生,我需要一些幫助。</p> <p>我對這里的兩種解決方案感到困惑,我應(yīng)該將 <strong>this.$route.params.id 作為字符串</strong> <em>還是這樣做更好</em> <strong>this.$ Route.params.id.toString()</strong></p>
P粉232409069
P粉232409069

全部回復(fù)(1)
P粉322106755

路由參數(shù)可以包含許多具有相同鍵的值。這就是為什么它可能是字符串?dāng)?shù)組而不是單個(gè)字符串。如果你確定只有一個(gè)參數(shù),你可以使用 as string 來告訴打字稿編譯器你知道這個(gè)變量 100% 是一個(gè) string 而不是 字符串[]代碼>

this.resetPassword(password1.value, password2.value, this.$route.params.id as string, this.$route.params.resetID as string)

如果您將使用 .toString() 并且會有一個(gè)像 ["foo", "bar"] 這樣的數(shù)組,您將得到 "foo ,bar" 作為 .toString()

的結(jié)果

如果不確定是否是數(shù)組,可以檢查一下,如果是數(shù)組則取第一個(gè)值:

let id: string;
if (Array.isArray(this.$route.params.id)) {
    id = this.$route.params.id[0];
} else {
    id = this.$route.params.id;
}
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板