路由參數(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()
如果不確定是否是數(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; }