我正在使用Vue3Vuetify,当为确认密码字段的v-textfield制定规则时,我遇到了这个错误:

I tried to compare the values of the password field and the one of the confirm password field, both associeted to the v-textfield with the v-model, but in the confirm password rule the variable of the password results undefined.
Here's the source.

Data whit rules and variables:

data: () => ({
  // Credentials
  username: '',
  password: '',
  confirmPassword: '',

  passwordRules: [
    (value) => {
      if (value?.trim().length > 0) return true
      else return 'Password is required.'
    },
  ],
  confirmPasswordRules: [
    (value) => {
      if (value?.trim().length > 0) {
        // TODO: ERROR (this.password is undefined)
        if(value === this.password) {
          return true
        }
        else return 'Passwords do not match.'
      }
      else return 'Confirm password is required.'
    },
  ],
}),

当我在v-textfield中写入字符时,错误总是出现,因为它在规则中:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'password')
    at confirmPasswordRules

谢谢!

推荐答案

OP正在将他的data关键写为

data: () => ({

这不是通常的正确语法

data() {

当归结到this的使用时,它确实没有同样的含义.

Javascript相关问答推荐

try 在addEventHandler内设置表单的文件输入.值=空

使用ReactJS对Ant Design表中的空值进行排序

如何避免使用ajax在Vue 3合成API中重定向

如何编辑代码FlipDown.js倒计时?

确定MutationRecord中removedNodes的索引

Klaro与Angular的集成

yarn安装一个本地npm包,以便本地包使用main项目的node_modules(ckeditor-duplicated-modules错误)

Javascript,部分重排序数组

如何从html元素创建树 struct ?

还原器未正确更新状态

如何在不影响隐式类型的情况下将类型分配给对象?

如何在JAVASCRIPT中临时删除eventListener?

如果没有页面重新加载Angular ,innerHTML属性绑定不会更新

第一项杀死下一项,直到数组长度在javascript中等于1

在D3条形图中对具有相同X值的多条记录进行分组

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

在Java脚本中录制视频后看不到曲目

如何在AG-Grid文本字段中创建占位符

Jexl to LowerCase()和Replace()

每隔3个项目交替显示,然后每1个项目交替显示