我正在使用 ref 來獲取一個值,并且我在型別 'Vue' 上不存在屬性 'value' 作為錯誤。
這是我的代碼
confirmPasswordRules: [
(v: string ) => !!v || "Password is required",
(v: string ) => (this.$refs.password) && (v === this.$refs.password.value || "Passwords do not match")
],
我使用 console.log 列印出 this.$refs.password.value 所以它確實有一個值。我也試圖(this.$refs.password as Vue & { password: () => string }).validate())驗證價值,但這沒有用。我需要做哪些改變?
uj5u.com熱心網友回復:
模板參考屬于 type unknown,因此它們需要作為目標元素的型別進行型別斷言。假設模板 ref 在 an 上<input>,則型別斷言將是as HTMLInputElement:
confirmPasswordRules: [
(v: string) => this.$refs.password && (v === (this.$refs.password as HTMLInputElement).value || "Passwords do not match")
---------------------------------------
??
],
uj5u.com熱心網友回復:
計算屬性在 onMount 事件之前被決議,這意味著模板部分還沒有呈現并且參考沒有到位。您可以在觸發操作之前檢查您的 ref 是否存在。
(v: string ) => (this.$refs?.password?.value) && (v === this.$refs.password.value || "Passwords do not match")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370699.html
