我正在处理Vue.js模板,在注册页面上,我需要在用户注册时比较密码,因此我添加了自定义验证规则,如下所示:
<v-text-field
label="Password"
v-model="password"
:rules="passwordRules"
type="password"
required
></v-text-field>
<v-text-field
label="Confirm Password"
v-model="confirmPassword"
:rules="[confirmPasswordRules,passwordConfirmationRule]"
type="password"
required
></v-text-field>
脚本:
data() {
return {
password: "",
confirmPassword: "",
passwordRules: [v => !!v || "Password is required"],
confirmPasswordRules: [v => !!v || "Password is required"],
};
},
在 computed 中比较密码的方法:
computed: {
passwordConfirmationRule() {
return () => (this.password === this.confirmPassword) || 'Password must match'
},
}
我使用computed方法来确认密码,它能够很好地比较并匹配密码,但是在控制台中显示错误
[Vuetify]规则应返回字符串或布尔值,而不是'对象'
,那么我该如何解决呢?
this.password
在confirmPasswordRules
中始终为空,你们知道为什么吗? - Sri:rules="[!!confirmPassword ||'输入确认密码', password === confirmPassword || '密码确认不匹配。']"
- Sri